Click here to Skip to main content
15,923,197 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I'm creating a new dialog box running with its own thread.
In the constructor of the dialog box, some UI components will be allocated, which are hosted under a canvas.

System.Threading.Thread thread = new System.Threading.Thread (() >
{
Dlg dlg = new Dlg ();
dlgVideo.Show ();
System.Windows.Threading.Dispatcher.Run ();
});

thread.SetApartmentState (System.Threading.ApartmentState.STA);
thread.Start ();



Then I try to modify some UI components.
dlg.Dispatcher.Invoke (new System.Action (() =>
{
dlg.DoSomeUIStuff ();
}));


The problem I have, is some cases, the call to dlg.Dispatcher.Invoke hangs for 15 to 20 seconds.
This really depends on the machine where I run the code. Sometimes, it doesn't hang, sometimes it does.

If I replace Invoke by BeginInvoke, then it won't hang anymore, but the command dlg.DoSomeUIStuff (); will be executed 15 to 20 seconds later.


Any clue?

What I have tried:

I try to replace Invoke by BeginInvoke, but it didn't solve the problem.
Posted
Comments
Richard Deeming 12-Apr-16 9:45am    
Something is running on the dialog's UI thread. You'll need to debug your code to find out what, and see if you can move it to a background thread.
Sergey Alexandrovich Kryukov 12-Apr-16 10:36am    
You don't need to Run() a dispatcher on your non-UI thread, or anywhere else. You need to call Dispatcher.Invoke on your UI thread. And who knows what you DoSomeUIStuff does? (Probably you do. :-)
—SA
Member 8833981 14-Apr-16 3:41am    
Thanks for your answers.

I found that an UI component causes the problem.
I'll look into the background thread solution as suggested.

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