I'm using Microsoft Solver Foundation
in my recent WinForms
project to solve a scheduling problem.
My scheduling method is something like this:
public class Scheduler
{
public void Schedule()
{
InitializeParameters();
PrepareDateFromDatabase();
ScheduleUsingMSF(); //<---- this line is black box and take a long time to execute
SaveSchedulingResultToDb();
}
}
Sometimes scheduling process takes long time(ScheduleUsingMSF() method that I don't have any control on it, take long time), I used a BackgroundWorker
to call my scheduling method to prevent GUI freezing.
When a scheduling process take long time users may wants to cancel current scheduling operation and change their parameters and run it again, so I want to provide a cancellation mechanism to them, so I used following code to cancel operation according to How to: Use a Background Worker:
bw.WorkerSupportsCancellation = true;
...
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
var scheduler = new Scheduler();
scheduler.Schedule();
}
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
{
bw.CancelAsync();
}
}
Where and how should I check if ((bw.CancellationPending == true))
to cancel Schedule()
method?