【问题标题】:how to cancel DoWork in Backgroundworker如何在Backgroundworker中取消DoWork
【发布时间】:2012-03-27 09:28:56
【问题描述】:

我知道这不是关于取消 BackGroundWorker 的第一个问题,但我没有找到解决问题的答案。
我有一个发送文件的方法..我使用 backgroundworker 来调用它..
那么我怎样才能在发送文件的中间取消 backgroundworker.. 我的意思是我应该放在哪里

if (backgroundWorker1.CancellationPending == true)
   {
       e.Cancel = true;
       break;
   }

代码如下:

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        List<object> job = (List<object>)e.Argument;
        string srcPath = (string)job[0];
        string destPath = (string)job[1];
        SendFile(srcPath, destPath);
    }

发送方法:

 private void SendFile(string srcPath, string destPath)
    {
        string dest = Path.Combine(destPath, Path.GetFileName(srcPath));
        using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
        {
            try
            {
                long fileSize = fs.Length;
                if (sizeAll == 0)
                    sizeAll = fileSize;
                sum = 0;
                int count = 0;
                data = new byte[packetSize];
                SendCommand("receive<" + dest + "<" + fs.Length.ToString());
                ProgressLabel(++fileCount, allFileCount);
                InfoLabel("Sending " + srcPath, "busy");
                while (sum < fileSize)
                {
                    count = fs.Read(data, 0, data.Length);
                    network.Write(data, 0, count);
                    sum += count;
                    sumAll += count;
                    backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
                }
                network.Flush();
            }
            finally
            {
                network.Read(new byte[1], 0, 1);
                CloseTransfer();
            }
        }
    }

我应该在 send 方法的 while() 循环中检查 CancellationPending .. 但我无法从这个方法中访问 [e.Cancel] 的 backgroundworker .. 我该怎么办?

【问题讨论】:

  • 将e作为参数从backgroundWorker1_DoWork传递给SendFile,然后在ReportProgress之后签入while循环有什么问题?你还有一个 CloseTransfer 的 finally 子句
  • @Steve .. 你的意思是像编辑 sendfile 方法: void SendFile(string srcPath, string destPath, DoWorkEventArgs e)
  • 是的,就是这样,只需向 SendFile 添加第三个参数

标签: c# backgroundworker cancellation


【解决方案1】:

DoWorkEventArgs e 可以作为 SendFile 中的第三个参数传递:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    List<object> job = (List<object>)e.Argument; 
    string srcPath = (string)job[0]; 
    string destPath = (string)job[1]; 
    SendFile(srcPath, destPath, e); 
} 

那么 SendFile 将是

private void SendFile(string srcPath, string destPath, DoWorkEventArgs e)   

在你的循环中,在 ReportProgress 之后

   if (backgroundWorker1.CancellationPending == true)    
   {    
       e.Cancel = true;    
       return; // this will fall to the finally and close everything    
   }   

【讨论】:

    【解决方案2】:

    把它放在你的while循环中:

    while (sum < fileSize)
    {
        if (worker.CancellationPending)
        {
            e.Cancel = true;
            break;
        }
    
        count = fs.Read(data, 0, data.Length);
        network.Write(data, 0, count);
        sum += count;
        sumAll += count;
        backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
    }
    network.Flush();
    

    【讨论】:

    • NOOO 重要的部分是 e.Cancel = true;
    • 没有e.Cancel!!我应该从 backgroundworker_dowork 传递 e 吗?
    猜你喜欢
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2021-01-03
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多