簡體   English   中英

我的后台工作線程阻塞了主線程 c#

[英]my background worker block the main thread c#

我正在嘗試使用 c# file up-loader 上傳許多文件,並使用戶能夠在中間停止該進程,因此我創建了后台工作程序以在其上運行上傳,但取消按鈕不起作用(它在所有文件上傳后觸發)和 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();
}

這種情況稱為競爭條件,是多線程編程中常見的問題。 有關多線程設計問題的更多信息,請參閱托管線程最佳實踐

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM