Results 1 to 5 of 5

Thread: [2.0] Application.DoEvents

  1. #1

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    [2.0] Application.DoEvents

    I'm primarily a web developer but have to dip into a few windows apps now and again as I am currently the only .net developer in my team.

    A former collegue of mine used the following snippet very frequently:
    Code:
    for(int i=0;i<10;i++)
    {
        Application.DoEvents();
    }
    From my understanding Application.DoEvents tells windows to process all messages currently in it's queue so what is the benefit of looping it 10 times?

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2.0] Application.DoEvents

    Application.DoEvents() processes all the message sin the queue so the first call is enough. The only time it would be necessary to call that continuously in a loop is if you were doing something else like so...

    Code:
    for(int i=0;i<10;i++)
    {
        //TxtBox1.Text = "sssssssssss";
        //TxtBox2.Text = "zzzzzzzzzzz";
        Application.DoEvents();
    }
    That is needed because it has to allow the UI to update, otherwise calling it once is adequate.

  3. #3

    Thread Starter
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: [2.0] Application.DoEvents

    Thanks, I thought as much. I can't work out why the previous developer did this.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2.0] Application.DoEvents

    I find that using Application.DoEvents() is a very...fishy way of keeping the UI responsive when executing big loops or something of the like.
    In my opinion you should never execute such code in the main thread but rather do it in a worker thread.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2.0] Application.DoEvents

    Quote Originally Posted by Fishcake
    Thanks, I thought as much. I can't work out why the previous developer did this.
    Maybe he thought you had to do it once for each potential message.

    I agree with Atheist on its use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width