Answer the question
In order to leave comments, you need to log in
Why does windwos forms form and timers freeze?
Hello.
There is a windows forms application that works from the tray. The tray icon hangs, the application works mostly in a minimized form.
The application uses several threads that work with the picturebox. Before changing the properties of the picturebox, I block it:
lock (pictureBox)
{
var updateAction = new Action(() =>
{
pictureBox.Image = s.Image;
}
}
Answer the question
In order to leave comments, you need to log in
Working with UI, that is, with forms and controls, from the thread that did NOT spawn this form / control is a very bad practice, which sooner or later leads to problems like yours. In addition, it also complicates debugging.
Regardless of what calculations you have there, perform them separately, add the result to an array / Bitmap, and already in the UI thread on a timer regularly read the calculated data and display it in the PictureBox.
Even if the problem persists, it will be easier for you to determine where the hang is occurring. Now it could be for a bunch of different reasons. I repeat: the fact that you lock pictureBox is not a solution to the problem - in WinAPI you CANNOT work with the UI directly from another thread, and WinForms is nothing more than a wrapper over standard controls.
What Stanislav Makarov is talking about is not entirely true because there are a lot of cases when you need to change the control from a third-party thread (so in general, yes - if it is possible to avoid changing the control in a third-party thread, avoid it). Your lock won't help here. That 's what Control.InvokeRequired is for .
(In general, when it comes to graphical applications, I advise you to switch from WinForms to WPF.)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question