This is how I set up my Background Worker
The question is: How do I terminate the process midway? I have to create a cancel button that will stop current processing.
I thought of use the line below...
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
...and encapsulating the AllocateTrades2 method inside, but that would mean I have to call the Cancel Flag from somewhere else, and it will check it only after the method is done processing.
And I can't just "pretend" that it is not doing anything because if the user clicks the same operation again, an exception will be thrown that the worker is in use.
An example of what I want to do:
public void bw_DoWork(object sender, DoWorkEventArgs e)
{
AllocateTrades2();
}
public void AllocateTrades()
{
Method1();
CheckTerminate();
Method2();
CheckTerminate();
//Click the cancel button in the UI during method 3 processing
Method3();
CheckTerminate();
}
And the CheckTerminate() stops all of its processing and makes itself available for future calls. Is what I'm trying to do possible?