DispatcherQueue.TryEnqueue 方法

定義

多載

名稱 Description
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

適用於