// This method starts an asynchronous calculation.
// First, it checks the supplied task ID for uniqueness.
// If taskId is unique, it creates a new WorkerEventHandler
// and calls its BeginInvoke method to start the calculation.
public void CalculatePrimeAsync(int numberToTest, Object taskId)
{
// Create an AsyncOperation for taskId.
AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(taskId);
synchronized (userStateToLifetime.get_SyncRoot())
{
// Multiple threads will access the task dictionary,
// so it must be locked to serialize access.
if (userStateToLifetime.Contains(taskId)) {
throw new ArgumentException("Task ID parameter must be unique",
"taskId");
}
userStateToLifetime.set_Item(taskId, asyncOp);
}
// Start the asynchronous operation.
WorkerEventHandler workerDelegate = new WorkerEventHandler(CalculateWorker);
workerDelegate.BeginInvoke(
numberToTest,
asyncOp,
null,
null);
}