为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕姐4208626 2019-09-02 15:37:02
我有一个类似于Greg D讨论的SafeInvoke Control扩展方法(减去IsHandleCreated检查)。我从System.Windows.Forms.Form以下地址调用它:public void Show(string text) {    label.SafeInvoke(()=>label.Text = text);    this.Show();    this.Refresh();}有时(此调用可能来自各种线程),这会导致以下错误: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发生了什么,我该如何解决?我知道这不是形式创建的问题,因为有时它会工作一次并且下次失败,那么问题是什么呢?PS。我真的很擅长WinForms,有没有人知道一系列很好的文章解释整个模型以及如何使用它?
查看完整描述

3 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

我发现InvokeRequired不可靠,所以我只是使用


if (!this.IsHandleCreated)

{

    this.CreateHandle();

}


查看完整回答
反对 回复 2019-09-02
  • 3 回答
  • 0 关注
  • 2864 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信