none
Application.Run versus Form.show and Application.DoEvents RRS feed

  • Question

  • Hello

    I'm wondering if the following two scripts have any underlying differences in terms of starting a gui winforms (or wpf) application:

    Application.Run(new MainForm());

    Versus

    new MainForm().Show(); While(true) { Application.DoEvents(); }

    Obviously there will be a difference in how you quit the application but my questions is not about that. It's about whether the Application.Run is fundamentally different than calling Application.DoEvents() repeatedly. I guess it should but I coudln't find the answer on the web.


    Tuesday, February 21, 2012 4:40 PM

Answers


  • It did clear things a bit but is that all the difference that the application.run has? is it just puttting the main thread to sleep when the message queu is empty?

    I do understand the bit about the exit code but that's irrelevant.

    It's a subtle difference - 

    Application.DoEvents does the same type of message pump as Application.Run, but Run() doesn't use a sleep - instead, it's using a wait handle that will block the main thread, but immediately wake it up when a message is received.  This has the effect of "sleeping" but not sleeping for a known length of time (like Thread.Sleep()), but instead "sleeping" just until a new message is received in the process.

    From the standpoint of execution, using something like:

    while(true)
    {
        Thread.Sleep(0);
        Application.DoEvents();
    }

    Will "appear" very similar to Application.Run - it will use more resources (the loop will run a LOT more than necessary), but it won't use 100% CPU and won't be noticibly different (other than the exit condition not being handled properly).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 21, 2012 6:46 PM
    Moderator

All replies

  • Application.Run() implements a message loop that returns only on reception of the WM_QUIT message or Application.Exit() or ExitThread().

    Application.DoEvents() is a message loop that returns as soon as all messages have been processed and the message queue is empty.

    Form.Show() merely makes the window visible.

    So from the above there are a couple of big differences:

    1. Application.Run() puts the thread to sleep if the message queue is empty; while(true) + DoEvents() will most likely spike the processor usage to 100%.
    2. while(true) + DoEvents() doesn't have an exit condition.

    Jose R. MCP

    Tuesday, February 21, 2012 4:45 PM
  • Thanks for your swift reply.

    It did clear things a bit but is that all the difference that the application.run has? is it just puttting the main thread to sleep when the message queu is empty?

    I know while(true) application.doevents will hike the processor usage but what about something like this:

    While(true)

    {

    Thread.Sleep(1000)

    Application.DoEvents()

    }

    I know it sounds a bit silly but this question relates to a long discussion we had in class about message pump and application class.

    I do understand the bit about the exit code but that's irrelevant.

    Tuesday, February 21, 2012 5:17 PM
  • If you add a call to Sleep() then the processor usage won't spike, but then you'll have a 1-second delay for processing messages which is unnecessary and could make an application seem jerky and unresponsive sometimes.  Application.Run(), on the other hand, will wake the thread on demand as soon as possible, meaning this jerky behavior won't be experienced.


    Jose R. MCP

    Tuesday, February 21, 2012 5:54 PM

  • It did clear things a bit but is that all the difference that the application.run has? is it just puttting the main thread to sleep when the message queu is empty?

    I do understand the bit about the exit code but that's irrelevant.

    It's a subtle difference - 

    Application.DoEvents does the same type of message pump as Application.Run, but Run() doesn't use a sleep - instead, it's using a wait handle that will block the main thread, but immediately wake it up when a message is received.  This has the effect of "sleeping" but not sleeping for a known length of time (like Thread.Sleep()), but instead "sleeping" just until a new message is received in the process.

    From the standpoint of execution, using something like:

    while(true)
    {
        Thread.Sleep(0);
        Application.DoEvents();
    }

    Will "appear" very similar to Application.Run - it will use more resources (the loop will run a LOT more than necessary), but it won't use 100% CPU and won't be noticibly different (other than the exit condition not being handled properly).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 21, 2012 6:46 PM
    Moderator