Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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)}
Posted

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:
C#
//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!";});

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

Regards,

Tam
 
Share this answer
 
Comments
unnikrishnan c 15-Feb-15 0:18am    
Dear Tam,
You are a great programmer. I used your modified to suit my form to solve a problem in threading which prevented me from updating label1. Great thanks to you.
Unnikrishnan, India.
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.
 
Share this answer
 
SQL
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)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900