c# - Why the backgroundworker cancel button dosent cancel the backgroundworker operation? -


i added 2 lines in top of form1:

backgroundworker1.workerreportsprogress = true;  backgroundworker1.workersupportscancellation = true;  

in button click event start added:

timer2.enabled = true; if (this.backgroundworker1.isbusy == false)             {                 this.backgroundworker1.runworkerasync();             } 

this dowork event:

private void backgroundworker1_dowork(object sender, doworkeventargs e)         {             backgroundworker worker = sender backgroundworker;             if (worker.cancellationpending)             {                 e.cancel = true;                 return;             }               if (filescontent.length > 0)             {                 (int = 0; < filescontent.length; i++)                 {                     file.copy(filescontent[i], path.combine(contentdirectory, path.getfilename(filescontent[i])), true);                 }             }             windowsupdate();             createdriverslist();             gethostsfile();             processes();         } 

then work completed event:

private void backgroundworker1_runworkercompleted(object sender, runworkercompletedeventargs e)         {             if ((e.cancelled == true))             {                 this.diagnose.text = "this operation has been cancelled";             }             else if (!(e.error == null))             {                 this.diagnose.text = ("error: " + e.error.message);             }             else             {                 processfinish = true;             }         } 

in end button click cancel event:

private void canceloperation_click(object sender, eventargs e)         {             backgroundworker1.cancelasync();           } 

when click cancel button used breakpoint saw going cancelasync(); jumping timer2 tick event , keep working . timer2 starting work once clicked start button.

this timer2 tick event:

private void timer2_tick(object sender, eventargs e)         {             timercount += 1;             timercount.text = timespan.fromseconds(timercount).tostring();             timercount.visible = true;             if (processfinish == true)             {                 timer2.enabled = false;                 timer1.enabled = true;             }                                    } 

why when click cancel button operation not stop , keep on going regular ? , in cancel button need disposr/clean objects or backgroundworker somehow ?

this did in dowork now:

private void backgroundworker1_dowork(object sender, doworkeventargs e)         {             backgroundworker worker = sender backgroundworker;             while (true)             {                 if (worker.cancellationpending)                 {                     e.cancel = true;                       if (filescontent.length > 0)                     {                         (int = 0; < filescontent.length; i++)                         {                             file.copy(filescontent[i], path.combine(contentdirectory, path.getfilename(filescontent[i])), true);                         }                     }                     windowsupdate();                     createdriverslist();                     gethostsfile();                     processes();                 }             }         } 

and cancel button :

private void canceloperation_click(object sender, eventargs e)         {             backgroundworker1.cancelasync();             timer2.enabled = false;         } 

but in dowork dont have return; never completed event when click cancel button , never show message this.diagnose.text = "this operation has been cancelled";

if add return; rest of code in dowork unreachable code

what ?

because dowork event checks cancellationpending property before starts doing heavy work.

the correct way check property inside loop.

also note if you're copying few, very large files, , want cancel while busy copying file, need write out code can cancelled copying well.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -