在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。 [英] Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

查看:84
本文介绍了在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我收到以下错误消息,但没有源跟踪,所以我无法找到错误。



这是随机发生的,在调试期间它只发生一周左右。

编译应用程序后,通常只在第一个应用程序启动时发生在一个新的启动后,之后编译的应用程序工作完美。



希望有人可以帮我找到错误点,或者至少是一个起点:)



谢谢



Hi,

I'm getting this error message below, but without a source trace, so i'm unable to locate the error.

This is happening randomly, during debugging it only happens one a week or so.
Once the application is compiled, it happens usually only the first application start after a fresh boot, and afterwards the compiled app works perfect.

Hopefully there is someone who can help me finding the error spot, or at least a starting point :)

Thanks

Message:
{"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."}

Source:
System.Windows.Forms

StackTrace:
   at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
   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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.HideSplashScreen()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.MainFormLoadingDone(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at KitchenPlan.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
   at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
   at System.Activator.CreateInstance(ActivationContext activationContext)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
   
TargetSite:
  {Void WaitForWaitHandle(System.Threading.WaitHandle)}

推荐答案



我在使用后台工作线程来更新某些UI组件时遇到了类似的问题。在C#(Vb应该是非常相似的语法)。在调用匿名委托更新UI组件之前,请添加以下检查:

Hi,
I have similar issue while using the Background Worker thread to update some UI components. In C# (Vb should be very similar syntax). Before calling the anonymous delegate to update your UI components, add the following check:
//Fixed window handle issue.
if (!m_form.IsHandleCreated)
    m_form.CreateControl();
//Update the label using delegate method
m_form.lblHello.Invoke((MethodInvoker)delegate(){ m_form.lblHello.Text = "Hello!";});



注意:m_form是windows表单,包含一个名为lblHello的标签要更新。我希望这有帮助。



问候,



Tam


Note: m_form is windows form contains a label named lblHello to update. I hope this help.

Regards,

Tam


根据跟踪,您正在从表单Load事件内部的控件上调用Invoke。不能可靠地做到这一点。可见的表单/控件还没有真正创建。也许你应该从Form.Activated或Form.Shown事件调用那些东西。如果需要,您可能需要输入一些代码来防止再次调用Invoke,因为与Form.Load不同,这些事件可以在表单的生命周期中多次引发。
According to the trace, you're calling Invoke on a control from inside the forms Load event. Can't do that reliably. The visible form/controls aren't really created yet.Perhaps you should be calling that stuff from the Form.Activated or Form.Shown events. You'll probably have to put in a bit of code to keep the Invoke from being called again if required, because, unlike Form.Load, these events can be raised multiple time in the lifetime of the form.


MSDN: "In the case where the control's handle has not yet been created, you should not simply call properties, methods, or events on the control. This might cause the control's handle to be created on the background thread, isolating the control on a thread without a message pump and making the application unstable." The whole point of the exercise is to avoid creating the handle on the wrong thread. If this call happens from a thread that isn't the gui thread, bang- you're dead.

(See http://ikriv.com/en/prog/info/dotnet/MysteriousHang.html)


这篇关于在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆