如何取消Backgroundworker中的DoWork任务

3

我知道这不是关于取消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();
            }
        }
    }

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


将e作为参数从backgroundWorker1_DoWork传递到SendFile有什么问题,然后在ReportProgress之后的while循环中进行检查?您还可以使用finally子句来CloseTransfer。 - Steve
@Steve..你的意思是修改sendfile方法为: void SendFile(string srcPath, string destPath, DoWorkEventArgs e) - Murhaf Sousli
5
是的,就是这样。只需向SendFile添加第三个参数即可。 - Steve
2个回答

8
在SendFile中,可以将DoWorkEventArgs e作为第三个参数传递:
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    
   }   

3

将其放置在 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();

1
不行,重要的部分是 e.Cancel = true; - Murhaf Sousli
没有 e.Cancel!! 我应该从 backgroundworker_dowork 传递 e 吗? - Murhaf Sousli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接