相关疑难解决方法(0)

在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke

我有一个类似于Greg D讨论的SafeInvoke Control扩展方法(减去IsHandleCreated检查).

我从System.Windows.Forms.Form以下地址调用它:

public void Show(string text) {
    label.SafeInvoke(()=>label.Text = text);
    this.Show();
    this.Refresh();
}
Run Code Online (Sandbox Code Playgroud)

有时(此调用可能来自各种线程),这会导致以下错误:

System.InvalidOperationException 发生了

Message="在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke."

Source="System.Windows.Forms"

StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at DriverInterface2.UI.WinForms.Dialogs.FormExtensions.SafeInvoke[T](T control, Action`1 action) 
in C:\code\DriverInterface2\DriverInterface2.UI.WinForms\Dialogs\FormExtensions.cs:line 16
Run Code Online (Sandbox Code Playgroud)

发生了什么,我该如何解决?我知道这不是形式创建的问题,因为有时它会工作一次并且下次失败,那么问题是什么呢?

PS.我真的很擅长WinForms,有没有人知道一系列很好的文章解释整个模型以及如何使用它?

multithreading winforms

76
推荐指数
4
解决办法
13万
查看次数

标签 统计

multithreading ×1

winforms ×1