multithreading - How to cancel cancellationtoken for the second time in C# project -


there 2 threads in c# project; ui thread , backgroundworker.

in backgroundworker there work such as;

private void testbgworker_dowork(object sender, doworkeventargs e)     {         backgroundworker worker = (backgroundworker)sender;         while (!worker.cancellationpending)         {             // work!             token.waithandle.waitone(20000);         }     } 

in ui thread;

// initialization cancellationtoken cancellationtokensource tokensource = new cancellationtokensource(); cancellationtoken token = tokensource.token;  // click event listener of button!     private void testbtn_click(object sender, eventargs e)         {            testbgworker.cancelasync();            tokensource.cancel();         } 

in ui thread, when user clicks button, backgroundworker starts running. if user clicks button second one, backgroundworker cancelled. there no problem cycle.

however, when user clicks again after backgroundworker cancelled, since iscancellationrequested true, thread not wait in line of token.waithandle.waitone(20000) , passed next line. this problem!

cannot use token multiple times? how make iscancellationrequested property of cancellationtoken false again second cycle? or have use new cancellationtoken?


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -