Click here to Skip to main content
13,354,605 members (62,687 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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 :)


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


   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()
  {Void WaitForWaitHandle(System.Threading.WaitHandle)}
Posted 19-Feb-13 22:04pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)
//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.


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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 26 Oct 2015
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100