Canceling Tasks

In some situations you want to cancel task execution. To programmatically cancel a task, you need to enable tasks for cancellation, which requires some lines of code. You need an instance of the System.Threading.CancellationTokenSource class; this instance tells a System.Threading.CancellationToken that it should be canceled. The CancellationToken class provides notifications for cancellation. The following lines declare both objects:

Dim tokenSource As New CancellationTokenSource()Dim token As CancellationToken = tokenSource.Token

Setting Time for Cancellation

The .NET Framework 4.6 enables you to create an instance of the Cancellation-TokenSource class and specify on such an instance how much time ...

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.