在维护严重违反 winforms 中的跨线程更新规则的旧应用程序的过程中,我创建了以下扩展方法,以在发现非法调用时快速修复它们:
/// <summary>
/// Execute a method on the control's owning thread.
/// </summary>
/// <param name="uiElement">The control that is being updated.</param>
/// <param name="updater">The method that updates uiElement.</param>
/// <param name="forceSynchronous">True to force synchronous execution of
/// updater. False to allow asynchronous execution if the call is marshalled
/// from a non-GUI thread. If the method is called on the GUI thread,
/// execution is always synchronous.</param>
public static void SafeInvoke(this Control uiElement, Action updater, bool forceSynchronous)
{
if (uiElement == null)
{
throw new ArgumentNullException("uiElement");
}
if (uiElement.InvokeRequired)
{
if (forceSynchronous)
{
uiElement.Invoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); });
}
else
{
uiElement.BeginInvoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); });
}
}
else
{
if (!uiElement.IsHandleCreated)
{
// Do nothing if the handle isn't created already. The user's responsible
// for ensuring that the handle they give us exists.
return;
}
if (uiElement.IsDisposed)
{
throw new ObjectDisposedException("Control is already disposed.");
}
updater();
}
}
示例用法:
this.lblTimeDisplay.SafeInvoke(() => this.lblTimeDisplay.Text = this.task.Duration.ToString(), false);
我也喜欢如何利用闭包来读取,尽管在这种情况下 forceSynchronous 需要为真:
string taskName = string.Empty;
this.txtTaskName.SafeInvoke(() => taskName = this.txtTaskName.Text, true);
我不质疑这种方法对修复遗留代码中的非法调用的有用性,但是新代码呢?
当您可能不知道哪个线程正在尝试更新 ui 时,使用此方法更新新软件中的 UI 是否是一个好的设计,或者新的 Winforms 代码通常应该包含一个特定的、专用的方法以及适当的Invoke()
相关管道所有这些用户界面更新?(当然,我会先尝试使用其他合适的后台处理技术,例如 BackgroundWorker。)
有趣的是,这不适用于ToolStripItems。我最近才发现它们直接来自Component而不是来自Control。相反,ToolStrip
应该使用包含的调用。
评论的跟进:
一些评论表明:
if (uiElement.InvokeRequired)
应该:
if (uiElement.InvokeRequired && uiElement.IsHandleCreated)
考虑以下msdn 文档:
这意味着 如果不需要 Invoke(调用发生在同一个线程上),或者如果控件是在不同的线程上创建但尚未创建控件的句柄,则 InvokeRequired 可以返回 false 。
在尚未创建控件句柄的情况下,不应简单地调用控件上的属性、方法或事件。这可能会导致在后台线程上创建控件的句柄,从而在没有消息泵的线程上隔离控件并使应用程序不稳定。
当 InvokeRequired 在后台线程上返回 false 时,您还可以通过检查 IsHandleCreated 的值来防止这种情况。
如果控件是在不同的线程上创建的,但尚未创建控件的句柄,则InvokeRequired
返回 false。这意味着如果InvokeRequired
返回true
,IsHandleCreated
将始终为真。再次测试它是多余的和不正确的。