private void InternalStart()
{
// Reset our state - we might be run more than once.
cancelledFlag = false;
completeFlag = false;
cancelAcknowledgedFlag = false;
failedFlag = false;
// isRunning is set during Start to avoid a race condition
try
{
DoWork();
}
catch (Exception e)
{
// Raise the Failed event. We're in a catch handler, so we
// had better try not to throw another exception.
try
{
FailOperation(e);
}
catch
{ }
// The documentation recommends not catching
// SystemExceptions, so having notified the caller we
// rethrow if it was one of them.
if (e is SystemException)
{
throw;
}
}
lock (this)
{
// If the operation wasn't cancelled (or if the UI thread
// tried to cancel it, but the method ran to completion
// anyway before noticing the cancellation) and it
// didn't fail with an exception, then we complete the
// operation - if the UI thread was blocked waiting for
// cancellation to complete it will be unblocked, and
// the Completion event will be raised.
if (!cancelAcknowledgedFlag && !failedFlag)
{
CompleteOperation();
}
}
}