2

I have a windows forms application where i add the mainloop in the constructor of the form like this:

Application.Idle += new EventHandler(Update);

that works fine - however, my update function is not called when I minimize the application window. What do I need to do in order to get my update function called also while the window is minimized?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
genesys
  • 21
  • 2

3 Answers3

3

You could call it from a System.Threading.Timer. Either start the Timer when you get minimized or just let it run (low frequency) and test for Minimized before calling Update.

Edit based on the comments

The most sensible way to do this is to run the Update code from another thread. The Idle-event or Timer solutions will both run into problems.

But with a thread you have to be careful when touching any UI Control. A simple approach is the BackgroundWorker. It has Completed and Progress events that are executed thread-safe.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Actually I just want to get my main update routine executed continuously. If I create a while(true){} loop in the programs main function, i run into Out of Memory exception since it seems that the garbage collector does not get invoked then.. – genesys Nov 27 '09 at 17:28
  • The Timer has a ~20ms resolution, if that is acceptable I would use that and forget Idle. But it sounds like you are burning a lot of CPU time this way. I wouldn't want that on my Laptop. – H H Nov 27 '09 at 17:36
  • I want the function to be called as often as possible, since it processes some images and this shall happen quickly (as quick as possible) – genesys Nov 27 '09 at 17:43
  • what will happen if I use System.threading.Timer with a time which is less than the function actually needs for execution? – genesys Nov 27 '09 at 17:44
1

You can use a timer as Henk suggests, but simply add a flag that signals that the method is still processing.

So, set a delay time of, say, 50ms, and code up the event handler like so:

private void timer1_Tick(object sender, EventArgs e)
{
    if ((this.WindowState == FormWindowState.Minimized) && !_isProcessing)
    {
        _isProcessing = true;

        // Do stuff

        _isProcessing = false;
    }
}

where _isProcessing is a private boolean variable on your form.

If you expect the operations to take less than 50ms each, then aggregate the operations somehow (maybe a queue would be appropriate).

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
0

I'd have to try this to be sure, but my first guess is to wire up the form Resize event, and in the handler, check to see if the form is minimized...

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • The question is: _while_ minimized. Not _when_ minimizing. – H H Nov 27 '09 at 17:22
  • 1
    @Henk, the OP says it doesn't work "when I minimize the application window" then later he says "... called also while the window is minimized", so I was/am not clear which he is interested in... Given that there seems to be little or no reason to be doing to be doing updates while the UI is minimized (What would be triggering the update??) I assumed he meant "when" it was being minimized... – Charles Bretana Nov 27 '09 at 21:21