gpt4 book ai didi

c# - 访问 UI 控件时是否应始终使用 Control.InvokeRequired

转载 作者:太空狗 更新时间:2023-10-29 21:18:30 25 4
gpt4 key购买 nike

我们正在使用 WinForms (3.5) 构建一个 .NET 应用程序。

我最近添加了一项新功能,在访问某些控件时开始遇到奇怪的行为。问题是一些 UI 控件访问只是停止了执行(没有看到异常)。

在仔细检查(使用 WinDbg)后,我意识到控件正在从 ThreadPool 线程更新,并且抛出了 CrossThreadMessagingException。

我的问题是 - 是否有关于如何规避此类行为的良好做法?

这将非常麻烦,但也可能无法使用 Control.Invoke 方法包围访问 UI 控件的每个代码位置。

我如何将我的代码划分为不应使用 Invoke 的“安全”代码和应该使用的代码?

最佳答案

如果应用程序被设计为多线程,则可能会发生跨线程,因此您需要使用 InvokeRequired 来检查它,或者使用您使用的方法在 UI 线程上调用 re-Invoke() 本身,或者抛出一个异常,表明代码使用不当。请记住,在某些情况下 InvokeRequired 将为 false(主要是当窗口没有句柄或正在处理/已处理时);防止这些情况的最佳方法是不要在窗口初始化过程中比 Load() 事件处理程序更早地启动线程,并通过取消窗口创建的后台线程并等待它们关闭来处理 Closing() 事件。

如果应用程序不是多线程的(您没有设置 BackgroundWorkers、TPL 操作、BeginInvoke()ing 委托(delegate)或 Start()ing 线程),则没有必要。但是,对 InvokeRequired 的调用非常便宜(其背后的逻辑基本上是检查 WinAPI 函数 GetThreadId 和 GetWindowThreadProcessId 是否返回相同的值),因此如果您预计程序被重组为多线程,则调用方法的以下模式很简单足以实现:

//no return value, no parameters; ShowWindow(), HideWindow(), etc
//Understand that many built-in control methods are not virtual and so you can't
//override them to do this; you must either hide them or ensure the caller is
//checking for cross-threading.
public void MyWindowMethod()
{
if(InvokeRequired)
this.Invoke(new Action(MyWindowMethod));
else
{
//main logic
}
}

//Input but no return; SetTitle("My Title")
public void MyWindowMethod2(string input)
{
if(InvokeRequired)
this.Invoke(new Action<string>(MyWindowMethod2), input);
else
{
//main logic
}
}

//inputs and outputs; custom methods, advanced graphics
public string MyWindowMethod3(string input)
{
if(InvokeRequired)
return (string)(this.Invoke(new Func<string, string>(MyWindowMethod3), input));

//No else required; the return makes it redundant
//main logic
}

关于c# - 访问 UI 控件时是否应始终使用 Control.InvokeRequired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com