cómo cancelar DoWork en Backgroundworker

i know this's not the first question about Canceling BackGroundWorker but i didn't find the answer to solve my problem.
i have a method that sends file .. im using backgroundworker to call it ..
So how can i cancel backgroundworker in the middle of sending the file .. i mean where should i put

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

aquí está el código:

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

Send 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 should check for CancellationPending in the while() loop in send method .. but i can't get to [e.Cancel] of backgroundworker from this method .. what shall i do?

preguntado el 09 de marzo de 12 a las 23:03

What's the problem to pass e as parameter from backgroundWorker1_DoWork to SendFile, then check in while loop after ReportProgress? You have also a finally clause to CloseTransfer -

@Steve .. you mean like edit sendfile method to : void SendFile(string srcPath, string destPath, DoWorkEventArgs e) -

Yes that's all Just add to SendFile a third argument -

2 Respuestas

The DoWorkEventArgs e could be passed as third argument in 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    
   }   

respondido 09 mar '12, 23:03

Ponlo en tu while bucle:

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

respondido 09 mar '12, 23:03

NOOO the important part is e.Cancel = true; - Murhaf Sousli

there's no e.Cancel !! should i pass the e from backgroundworker_dowork ? - Murhaf Sousli

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.