DerApe
DerApe

Reputation: 3175

Control.Invoke() stucks

I know there are several threads concerning this topic, but I think mine is different. In my application I open a form where the user can input some parameters for a upcomming printing. This printing is supposed to be run in a background worker. So I fire that background worker with the event "OnFormClosing".

Within that background worker I need to access the GUI and change/read it, so I need a control.Invoke(). "Sometimes" the Invoke keeps stuck at the invoke call itself and doesn't execute the delegate. My main thread is working fine and is not blocked. I still can interact with the GUI doing other stuff. Before posting any code: Are there any other conditions for executing a control.Invoke() other than

The main thread doesn't need to be free and exactly the invoke is called correct? It should continue once the main thread is idle...

Thanks for any help

Update:

Here is the thread situation during that issue: enter image description here The Main thread is executing this:

Application.Run(appContext);

So it is idle. The worker thread is waiting at this line:

fileName = (string)cbPrintFile.Invoke(new Func<String>(() => cbPrintFile.Text));

which is not executed like I state above. cbPrintFile is a combobox

Upvotes: 1

Views: 1249

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062512

Invoke is "enqueue and wait for it to be processed". If it is becoming "stuck", that suggests that you have deadlocked, for example because the UI thread is still in an event-handler waiting on the worker. If the code is properly de-coupled, you can probably replace the Invoke with BeginInvoke, which allows the worker to continue after queuing the work. Of course, it would also be good to ensure that the UI is never waiting on a worker. This can be done accidentally if trying to hold a lock (on the same object) in both places. You can investigate simply by pausing the application, pressing ctrl+d,t to bring up the threads, and ctrl+d,c to see the call-stack of each in turn.

Upvotes: 3

Related Questions