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

时间:2013-04-19 19:04:28

标签: .net vb.net multithreading winforms invoke

我正在执行以下代码:

grid.Invoke(New MethodInvoker(AddressOf RefreshDataSource))

获得InvalidOperationException例外。

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

堆栈追踪:

   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 DataForm.BindData() in D:\Proj\DataForm.vb:line 380
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)

3 个答案:

答案 0 :(得分:0)

将调用从load事件更改为form.shown事件,以确保首先创建控件

答案 1 :(得分:0)

.modalBox

答案 2 :(得分:0)

在调用调用方法while (!this.IsHandleCreated) System.Threading.Thread.Sleep(100)

之前添加它