DispatcherQueue.TryEnqueue 方法

定义

重载

TryEnqueue(DispatcherQueueHandler)

将任务添加到 DispatcherQueue ,该任务将在与 DispatcherQueue 关联的线程上执行。

TryEnqueue(DispatcherQueuePriority, DispatcherQueueHandler)

DispatcherQueue 添加一个任务,该任务将在具有指定优先级的 DispatcherQueue 关联的线程上执行。

TryEnqueue(DispatcherQueueHandler)

将任务添加到 DispatcherQueue ,该任务将在与 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

参数

callback
DispatcherQueueHandler

要执行的任务的委托。

返回

Boolean

bool

如此 如果 任务已添加到队列。 否则为 false

属性

示例

以下示例演示如何创建新线程并初始化 DispatcherQueueController 并对其运行 DispatcherQueue 事件循环。

// 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.
        });

注解

队列将串行调用 回调

调用 ShutdownQueueAsync () 后,队列不会对新任务进行排队,此方法将返回 false

适用于

TryEnqueue(DispatcherQueuePriority, DispatcherQueueHandler)

DispatcherQueue 添加一个任务,该任务将在具有指定优先级的 DispatcherQueue 关联的线程上执行。

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

参数

priority
DispatcherQueuePriority

任务的优先级 (,例如低、正常或高) 。

callback
DispatcherQueueHandler

要执行的任务的委托。

返回

Boolean

bool

如此 如果 任务已添加到队列。 否则为 false

属性

示例

以下示例演示如何创建新线程并初始化 DispatcherQueueController 并对其运行 DispatcherQueue 事件循环。

// 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.
        });

注解

队列将按优先顺序串行调用 回调

调用 ShutdownQueueAsync () 后,队列不会对新任务进行排队,此方法将返回 false

适用于