簡體   English   中英

Dispatcher Priority,DispatcherTimer,DispatcherFrame的用途是什么

[英]What is the use of Dispatcher Priority, DispatcherTimer, DispatcherFrame

我有一個異步DataGrid加載功能。 因此,我需要調用WaitFor()。 這是代碼:

WaitFor(TimeSpan.Zero, DispatcherPriority.SystemIdle);

以下是兩種方法。 有人可以解釋這種方法到底在做什么嗎?

public static void WaitFor(TimeSpan time, DispatcherPriority priority)
{
    DispatcherTimer timer = new DispatcherTimer(priority);
    timer.Tick += new EventHandler(OnDispatched);
    timer.Interval = time;
    DispatcherFrame dispatcherFrame = new DispatcherFrame(false);
    timer.Tag = dispatcherFrame;
    timer.Start();
    Dispatcher.PushFrame(dispatcherFrame);
}

public static void OnDispatched(object sender, EventArgs args)
{
    DispatcherTimer timer = (DispatcherTimer)sender;
    timer.Tick -= new EventHandler(OnDispatched);
    timer.Stop();
    DispatcherFrame frame = (DispatcherFrame)timer.Tag;
    frame.Continue = false;
}

您不需要任何WaitFor()。 為什么還要等什么呢? 只需讓UI線程解凍,一旦數據加載,DataGrid將顯示它們。

您發布的方法正在執行... WaitFor機制。 方法名稱說明了一切:)

以下是一些詳細信息:

DispatcherTimer是一個簡單的啞計時器,您可能已經在基本C#中知道了,只是一旦調用tick方法,它就會直接在UI線程上執行,因此您不必關心是否在UI線程上。 你永遠是:)

DispatcherTimer具有屬性,如果將屬性設置為高,則將在間隔之后立即調用滴答調用方法。 如果將屬性設置為“背景”,則在UI線程不忙時將調用tick方法。

DispatcherFrame是您當前所在的作用域。每個Displatcher操作都具有某種作用域。 每個范圍處理待處理的工作項

人們大量使用WinForms時,Dispatcher.PushFrame與DoEvent()相同。 為了使DoEvent變得簡單,您正在強制UI線程執行某項操作。

總結一下,您需要等待UI線程完成操作。

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM