2

I have a program that is used by Clients world wide. I check my error logs and quite a few seem to be having an exception (listed below) thrown that I can't really figure out or trace.

I have some invokes but they are all protected by InvokeRequired. Now I'm thinking, if i should use use the if (HandleCreated) instead.

I am not even sure where or when the exception is thrown.

In start up, after the InitializeComponent();, I have some tasks that require access to some controls such as datagridview. However, Like I said, I try to protect them with InvokeRequired. I am not sure if that's the place causing the problem.

What are the suggestions I could perform so try and trace this problem?

Anyway, this is my exception:

    System.InvalidOperationException: 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 ..()
   at ..()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 

ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

2 Answers 2

1

No, that cannot happen if you use InvokeRequired. It will only be true when the Handle is valid. Very simple avoid to anyway, just don't subscribe the event or start the thread until the Load event fires.

This crash occurs when the form closes. Something you cannot see in the stack trace because that happens on another thread. There's a race condition in InvokeRequired + Begin/Invoke(). InvokeRequired might return true and a microsecond later the form closes. Your Begin/Invoke call will fail with this exception.

This is not a race you can solve. You must ensure that the thread can no longer call BeginInvoke() before allowing the form to close. Which invariably means you have to prevent the form from closing. Background info is in this answer.

0

I'm not sure if trace would totally help you here. It's obvious somewhere in you code you are calling invoke/BeginInvoke before the handle has been created. Now, what I'm suggesting may require some work but you'll nail the caller that is prematurely invoking a non-created handle. I used this technique when trying to trace a lock/unlock threading issue in some old C++ legacy code in production. It worked so well, I just left it as is.

Here's my technique. Create an Extension class that accepts objects that support Invoke/BeginInvoke and EndInvoke. It may look something like this:

public static class MyInvokeExtension
{
   public static void TempInvoke(this objectthatsupportsinvoke, ...)
   {
        try
        {
           objectthatsupportsinvoke.Invoke(...);
        }
        catch(Exception ex)
        {
           Console.WriteLine();  // put a break-point here
        }
   }

   // add other BeginInvoke and EndInvoke methods and do the same as above.
}
  • Now do a search and replace throughout your code and replace all Invoke like calls with TempInvoke like calls.
  • Run you application in debug.
  • At some point you will hit a break point.
  • Use the call-stack window to find who is calling your invoke method earlier than they should.

I know this is a lot of work but its worth it in the long run, trust me. In fact, you can even use this code to validate that the object can handle invokes at a given point in time.

Let me know how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.