V
V
Vladimir Merk2015-10-19 17:07:31
.NET
Vladimir Merk, 2015-10-19 17:07:31

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;
      }
   }

The essence of the problem:
The application works in the tray for several hours - when you try to deploy it, the window expands and everything "freezes".
The logs show that everything is going well before deployment. The window freezes completely, including all the timers in it, does not respond to dragging, closing, full screen rotation - while the state of the application is "Running" in the processes.
BUT the most interesting thing is that when you call the context menu from the tray icon, everything is defrosted, and depending on the idle time, all events that should have been executed at that time are triggered.
For example, there is a timer that causes an event every second. If we call the menu from the tray 60 seconds after the "freeze", everything will unfreeze and the timer event will instantly execute 60 times. This behavior is only in the compiled version of the application, if you run it from the Visual Studio shell, then this effect cannot be achieved, so there is a difficulty in catching this bug.
What to search? Which way to look?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-10-19
@VladimirMerk

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.

T
tex0, 2015-10-20
@tex0

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 question

Ask a Question

731 491 924 answers to any question