I made a program to test Parallel programming in C# 4.0. However, there is a problem and I can't figure out why.
I write a method
private void compute(int startValue, int endValue, ConcurrentBag<Pair> theList)
{
try
{
Task computation = Task.Factory.StartNew(() => { findFriendlyNumbers(startValue, endValue, theList, tokenSource.Token); }, tokenSource.Token);
Task.WaitAll(computation);
StringBuilder builder = new StringBuilder();
foreach (Pair p in theList)
{
builder.AppendLine(p.ToString());
}
this.textBlockResult.Dispatcher.Invoke(new Action(() =>
{
this.textBlockResult.Text = builder.ToString();
this.progressBar1.Visibility = System.Windows.Visibility.Hidden;
}));
}
catch (AggregateException aEx)
{
MessageBox.Show("Entering"); //For debug, but never runs
aEx.Handle(handleCancelling);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Which is run on a separate thread (other than the UI thread).
And the function (simplified)
private void findFriendlyNumbers(int start, int end, ConcurrentBag<Pair> list, CancellationToken token)
{
//some initialization and computation
for (int i = start; i <= end; i++)
{
//check whether it's cancelled
token.ThrowIfCancellationRequested();
//some computation
}
}
The problem is, when the tokenSource is canceled, there will be an error "OperationCanceledException is not handled by user code", as if the catch block doesn't exist. I am not sure why, because my code is similar to that in textbook and also in MSDN.
Thank you.
EDIT: I actually wrote a similar program less than a month ago, and everything was fine then. Today I tried to run it again, the same problem happened. I installed Microsoft Visual Web Developer 2010 Express after I finished the program, and I am not sure whether this is the reason. I don't get it, the same code, different results.
EDIT: I thought about this problem and found where is wrong. In the past, I used to use "run without debugging" while I use debug now. Running without debugging solves the problem. And I will appreciate it if someone tells me why debug is different from "run without debug".