Canceling background worker
-
I am unsure the best way to cancel my background worker (bgw). My application (form1) has an options screen (form2). On app load, a background worker starts to ping a list of IPs. I would like to go into my options form and tell my app a different list of IPs to ping. Once I make this change, i need to stop the current bgw from pinging, reload the list, and start pinging again. I know that I first have to set the WorkerSupportsCancelation property to true, which should set CancellationPending to true, then call the CancelAsync method, check the CancellationPending, then cancel it as this article suggests. My issue is that I am canceling the bgw from the second form and do not currently have a 'DoWorkEventArgs' object to cancel. What am I missing to get this object and cancel the bgw? Thanks!
-
I am unsure the best way to cancel my background worker (bgw). My application (form1) has an options screen (form2). On app load, a background worker starts to ping a list of IPs. I would like to go into my options form and tell my app a different list of IPs to ping. Once I make this change, i need to stop the current bgw from pinging, reload the list, and start pinging again. I know that I first have to set the WorkerSupportsCancelation property to true, which should set CancellationPending to true, then call the CancelAsync method, check the CancellationPending, then cancel it as this article suggests. My issue is that I am canceling the bgw from the second form and do not currently have a 'DoWorkEventArgs' object to cancel. What am I missing to get this object and cancel the bgw? Thanks!
Once a thread is started and stopped, it cannot be restarted. You're best bet is to cancel the existing operation, and let the thread die. Create a NEW background worker, just the way you already, did and supply it with the new list.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007