c# - 我的后台工作人员阻塞了主线程 c#
问题描述
我正在尝试使用 c# file up-loader 上传许多文件并使用户能够在中间停止进程,因此我创建了 backgroundworker 以在其上运行上传,但取消按钮不起作用(它在所有文件上传后触发)并且 WorkerThread_ProgressChanged 不会影响 UI 元素并且标签文本不会改变,这是我的代码
protected void cancelupload_Click(object sender, EventArgs e)
{
workerThread.CancelAsync();
if (workerThread.CancellationPending)
{
}
}
private void WorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Uploadpercentage.Text= "Uploading... (" + e.ProgressPercentage + "%)";
}
private void WorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
// lblStopWatch.Text = "Cancelled";
}
else
{
// lblStopWatch.Text = "Stopped";
}
}
private void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
DateTime startTime = DateTime.Now;
_keepRunning = true;
List<KeyValuePair<string, string>> Unuploaded_files = new List<KeyValuePair<string, string>>() { };
int count = 0;
while (_keepRunning && count < fileCollection.Count)
{
HttpPostedFile uploadfile = fileCollection[count];
String fileName = Path.GetFileName(uploadfile.FileName);
string fileExxtension = Path.GetExtension(uploadfile.FileName);
if (ValidExtensions.Contains(fileExxtension))
{
if (File.Exists(Chosen_Site_Path + @"\" + Selected_folder_name.SelectedItem.Text + @"\" + fileName))
{
KeyValuePair<string, string> Unuploaded_file = new KeyValuePair<string, string>(fileName, "Another file exists with the same name!");
Unuploaded_files.Add(Unuploaded_file);
}
else
{
uploadfile.SaveAs(Chosen_Site_Path + @"\" + Selected_folder_name.SelectedItem.Text + @"\" + fileName);
log.INSERT_ACTIVITY_LOG(Session["User_PK"].ToString(), Session["User_Type"].ToString(), "Uploader Home Page : Uploaded " + fileName + "Selected site " + BASF_SITE_ID);
log.INSERT_SITE_HISTORY(Session["User_PK"].ToString(), BASF_SITE_ID, "Uploaded file: " + fileName);
}
}
else
{
KeyValuePair<string, string> Unuploaded_file = new KeyValuePair<string, string>(fileName, "File type is not allowed");
Unuploaded_files.Add(Unuploaded_file);
}
count++;
string timeElapsedInstring = (DateTime.Now - startTime).ToString(@"hh\:mm\:ss");
int percent = (int)(((Decimal)count / fileCollection.Count ) *100);
workerThread.ReportProgress(percent, timeElapsedInstring);
if (workerThread.CancellationPending)
{
// this is important as it set the cancelled property of RunWorkerCompletedEventArgs to true
e.Cancel = true;
break;
}
}
if (Unuploaded_files.Count == 0)
{
string message = "alert('Files Uploaded')";
ScriptManager.RegisterClientScriptBlock(submitupload, this.GetType(), "alert", message, true);
}
else
{
string msg = "UNUPLOADED FILES:" + @"\" + "n";
for (int i = 0; i < Unuploaded_files.Count; i++)
{
msg += Unuploaded_files[i].Key + ": " + Unuploaded_files[i].Value + @"\" + "n";
}
string alertmsg = "alert('" + msg + "')";
ScriptManager.RegisterClientScriptBlock(submitupload, this.GetType(), "alert", alertmsg, true);
}
viewSites();
}
解决方案
这种情况称为竞争条件,是多线程编程中常见的问题。有关多线程设计问题的更多信息,请参阅托管线程最佳实践。
推荐阅读
- html - 使用应贴在图片上的自定义 div 标记图像
- java - 如何使用 BufferedReader 输入实现 do...while
- nlp - 带有 ignore_index 的 Pytorch 损失函数导致模型不收敛
- javascript - 如何根据月份过滤掉数据
- google-chrome - 在chrome中模拟socket.io临时断开连接
- java - 如何向片段活动添加将打开新活动的 onClickListener
- spring - 如何在非 Spring 托管 bean 中使用 @Transactional?
- azure - 在Jenkins中使用PsExec,即使脚本失败,也显示Success
- bash - 我怎么能在gmail地址中做到这一点不能是两个或更多“。”(点)?
- elasticsearch - 失败的 Elasticsearch 重新索引的超时响应