Delen via


DispatcherQueue.TryEnqueue Method

Definition

Overloads

TryEnqueue(DispatcherQueueHandler)

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

TryEnqueue(DispatcherQueuePriority, DispatcherQueueHandler)

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

TryEnqueue(DispatcherQueueHandler)

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

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

Parameters

callback
DispatcherQueueHandler

The task to execute.

Returns

Boolean

bool

True indicates that the task was added to the queue; false, otherwise.

Attributes

Examples

// 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 callback is guaranteed to be invoked first despite Priority on the
// newly created thread.

bool isQueued = _queue.TryEnqueue(
        () =>
        {
            // task to perform on another thread.
        });

Remarks

The task will be queued at Normal priority. 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

TryEnqueue(DispatcherQueuePriority, DispatcherQueueHandler)

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

public:
 virtual bool TryEnqueue(DispatcherQueuePriority priority, DispatcherQueueHandler ^ callback) = TryEnqueue;
/// [Windows.Foundation.Metadata.Overload("TryEnqueueWithPriority")]
bool TryEnqueue(DispatcherQueuePriority const& priority, DispatcherQueueHandler const& callback);
[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 indicates that the task was added to the queue; false, otherwise.

Attributes

Examples

// 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