DispatcherQueue.TryEnqueue Method

Definition

Overloads

TryEnqueue(DispatcherQueueHandler)

Adds a task to the DispatcherQueue that will be executed on the thread associated with the DispatcherQueue.

TryEnqueue(DispatcherQueuePriority, DispatcherQueueHandler)

Adds a task to the DispatcherQueue that will be executed on the thread associated with the DispatcherQueue with the specified priority.

TryEnqueue(DispatcherQueueHandler)

Adds a task to the DispatcherQueue that will be executed on the thread associated with the DispatcherQueue.

public:
 virtual bool TryEnqueue(DispatcherQueueHandler ^ callback) = TryEnqueue;
[Windows.Foundation.Metadata.Overload("TryEnqueue")]
public bool TryEnqueue(DispatcherQueueHandler callback);
function tryEnqueue(callback)
Public Function TryEnqueue (callback As DispatcherQueueHandler) As Boolean

Parameters

callback
DispatcherQueueHandler

A delegate to the task to execute.

Returns

Boolean

bool

True if the task was added to the queue. Otherwise, false.

Attributes

Examples

The following example shows how to create a new thread and initialize a DispatcherQueueController and run a DispatcherQueue event loop on it.

// Create a new thread and initialize a DispatcherQueueController
// and run a DispatcherQueue event loop on it.
_queueController =
    DispatcherQueueController.CreateOnDedicatedThread();
_queue = _queueController.DispatcherQueue;

// This is the first TryEnqueue() after creating the DispatcherQueue. The
// first TryEnqueue task is guaranteed to be invoked first on the new
// thread.
bool isQueued = _queue.TryEnqueue(
        () =>
        {
            // task to perform on another thread.
        });

Remarks

The queue will invoke callback serially.

Once ShutdownQueueAsync() has been called, the queue will not queue new tasks and this method will return false.

Applies to

TryEnqueue(DispatcherQueuePriority, DispatcherQueueHandler)

Adds a task to the DispatcherQueue that will be executed on the thread associated with the DispatcherQueue with the specified priority.

public:
 virtual bool TryEnqueue(DispatcherQueuePriority priority, DispatcherQueueHandler ^ callback) = TryEnqueue;
[Windows.Foundation.Metadata.Overload("TryEnqueueWithPriority")]
public bool TryEnqueue(DispatcherQueuePriority priority, DispatcherQueueHandler callback);
function tryEnqueue(priority, callback)
Public Function TryEnqueue (priority As DispatcherQueuePriority, callback As DispatcherQueueHandler) As Boolean

Parameters

priority
DispatcherQueuePriority

The priority of the task (such as Low, Normal, or High).

callback
DispatcherQueueHandler

A delegate to the task to execute.

Returns

Boolean

bool

True if the task was added to the queue. Otherwise, false.

Attributes

Examples

The following example shows how to create a new thread and initialize a DispatcherQueueController and run a DispatcherQueue event loop on it.

// Create a new thread and initialize a DispatcherQueueController
// and run a DispatcherQueue event loop on it.
_queueController =
    DispatcherQueueController.CreateOnDedicatedThread();
_queue = _queueController.DispatcherQueue;

// This is the first TryEnqueue() after creating the DispatcherQueue. The
// first TryEnqueue task is guaranteed to be invoked first on the new
// thread, regardless of what priority it was enqueued at.
bool isQueued = _queue.TryEnqueue(Windows.System.DispatcherQueuePriority.High,
        () =>
        {
            // task to perform on another thread.
        });

Remarks

The queue will invoke callback serially and in priority order.

Once ShutdownQueueAsync() has been called, the queue will not queue new tasks and this method will return false.

Applies to