Winforms 跨線程更新 UI
在 Winforms 中, 所有的控件都包含 InvokeRequired 屬性, 如果我們要更新UI,通過它我們可以判斷是否需要調用 [Begin]Invoke.
直接使用
delegate void SetTextCallback(string text);
public void SetText(string text)
{
if (InvokeRequired)
{
var d = new SetTextCallback(SetText);
this.textBox1.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
直接調用 SetText 即可。
使用擴展方法
public static class MyClass
{
public static void InvokeIfRequired(this ISynchronizeInvoke obj, MethodInvoker action)
{
if (obj.InvokeRequired)
{
var args = new object[0];
obj.Invoke(action, args);
}
else
{
action();
}
}
}
使用:
this.textBox1.InvokeIfRequired(() =>
{
// Do anything you want with the control here
this.textBox1.Text = "text";
});
WPF 跨線程更新 UI
在 WPF 中,與 WinForms 中 InvokeRequired 屬性相對應的有: DispatcherObject.CheckAccess() 和 Dispatcher.CheckAccess().
// Uses the Dispatcher.CheckAccess method to determine if
// the calling thread has access to the thread the UI object is on.
private void TryToUpdateButtonCheckAccess(object uiObject)
{
Button theButton = uiObject as Button;
if (theButton != null)
{
// Checking if this thread has access to the object.
if (theButton.Dispatcher.CheckAccess())
{
// This thread has access so it can update the UI thread.
UpdateButtonUI(theButton);
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new UpdateUIDelegate(UpdateButtonUI), theButton);
}
}
}
然而, CheckAccess 和 VerifyAccess 被標記為永遠不在智能提示在顯示,是否在暗示我們應該直接使用 Dispatcher.Invoke() 呢?
//
// 摘要:
// 確定調用線程是否為與此 System.Windows.Threading.Dispatcher 關聯的線程。
//
// 返回結果:
// 如果調用線程是與此 System.Windows.Threading.Dispatcher 關聯的線程,則為 true;否則為 false。
[EditorBrowsable(EditorBrowsableState.Never)]
public bool CheckAccess();
相關鏈接:
