Control.Invoke () application is hanging

I am showing animation while my control is loading data. When the stream ends, I hide the animation and show the control. Therefore, I am executing this code from a thread:

protected void InvokeEnableBackControl() { if (this.InvokeRequired) { this.Invoke(new OpHandler(EnableBackControl)); } else { EnableBackControl(); } } 

Sometimes, when I execute this code, the main thread gets stuck in the following code:

 protected virtual void EnableBackControl() { if (overlayAnimation.TargetControl != null) { overlayAnimation.TargetControl.BringToFront(); } overlayAnimation.SendToBack(); overlayAnimation.Enabled = false; overlayAnimation.Visible = false; 

}

I'm not sure if he hung up the Enable or Visible parameter. Do you know any circumstances that an application that calls these properties can pass with Control.Invoke ?

+7
source share
5 answers

Note that Control.Invoke is synchronous, so it will wait for EnableBackControl() to return. Think of it using Control.BeginInvoke , which you can shoot and forget.

See this answer: What is the difference between Invoke () and BeginInvoke ()

+15
source

I had problems until I execute .Invoke in the background thread while my main thread is still busy. This gives the impression that the application freezes because .Invoke just sits there, waiting to respond that it pays attention. Possible reasons:

  • Your main thread is blocked, waiting for something.
  • Your main form currently had a modal dialog, so it did not listen for new requests.
  • Your main thread is spinning, or constantly checking that something is finished or something new. In my case, the main thread spent the first minute scrolling through the background threads in a narrow loop, so it did not listen for any .Invoke requests from the background threads.

When you attach a debugger, pay particular attention to what your main MessagePump control flow does — I suspect that its lack of attention is causing your problems. If you determine that this is a fuzzy loop in the main thread that is not responding, try inserting .DoEvents in the loop, which pauses execution and forces the main thread to clear the message pump and route any remaining requests.

+7
source

Run debugging, start the application, and then pause debugging in Visual Studio and check the threads.

+3
source

I found that actually drawing / drawing controls can be quite slow, especially if you have a lot of them, and / or use double buffering for a smooth update. I used BeginInvoke to update the listview control from the data I received from the socket. From time to time, updates occurred so quickly that they froze the application. I solved this by writing everything that I got on async sockets to get into the queue, and then in a separate thread uninstalling the data and using BeginUpdate and EndUpdate in the list and making all the outstanding updates between them. This cut a ton of extra redrawing and made the application much more responsive.

0
source

You should use BeginInvoke inested Invoke see link

0
source

All Articles