Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tokens for cancellation
- ParallelOptions po = new ParallelOptions();
- po.CancellationToken = cts.Token;
- try
- {
- Parallel.ForEach(queries, po, (currentQuery) =>
- {
- // Execute query
- ExecuteQuery(currentQuery);
- // Throw exception if cancelled
- po.CancellationToken.ThrowIfCancellationRequested(); // ***
- });
- }
- catch (OperationCanceledException cancelException)
- {
- Console.WriteLine(cancelException.Message);
- }
- System.OperationCanceledException was unhandled by user code
- Message=The operation was canceled.
- Source=mscorlib
- StackTrace:
- at System.Threading.CancellationToken.ThrowIfCancellationRequested()
- at CraigslistReader.SearchObject.<>c__DisplayClass7.<bw_DoWork>b__5(Query currentQuery) in {PATH}:line 286
- at System.Threading.Tasks.Parallel.<>c__DisplayClass2d`2.<ForEachWorker>b__23(Int32 i)
- at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
- InnerException:
Add Comment
Please, Sign In to add comment