Edit

Share via


DispatchSource.Cancel Method

Definition

Asynchronously cancels the dispatch source.

public void Cancel ();
member this.Cancel : unit -> unit

Remarks

The Cancel() function asynchronously cancels the dispatch source, preventing any further invocation of its event handler block. Cancellation does not interrupt a currently executing handler block (non-preemptive). If a source is canceled before the first time it is resumed, its event handler will never be called.  (In this case, note that the source must be resumed before it can be released.)

The IsCanceled function may be used to determine whether the specified source has been canceled.

When a dispatch source is canceled its optional cancellation handler will be submitted to its target queue. The cancellation handler may be specified via SetCancelHandler(Action). This cancellation handler is invoked only once, and only as a direct consequence of calling Cancel().

Important: a cancellation handler is required for file descriptor and mach port based sources in order to safely close the descriptor or destroy the port. Closing the descriptor or port before the cancellation handler has run may result in a race condition: if a new descriptor is allocated with the same value as the recently closed descriptor while the source's event handler is still running, the event handler may read/write data to the wrong descriptor.

Applies to