How to undo DoWork in Backgroundworker

I know this is not the first question about canceling BackGroundWorker, but I haven't found an answer to solve my problem.
I have a method that sends a file .. im using a backgroundworker to call it.
So how can I cancel the background worker in the middle of a file upload .. I mean where should I put

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

      

here's the code:

 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);
    }

      

Submit method:

 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();
            }
        }
    }

      

I have to check CancellationPending in while () loop in submit method. But I can't get to the [e.Cancel] background worker from this method .. what should I do?

+3


source to share


2 answers


DoWorkEventArgs e can be passed as the third argument to 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); 
} 

      

then SendFile will be



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

      

and in your loop after ReportProgress

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

      

+8


source


Place it in a loop 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();

      

+3


source







All Articles