none
Application.DoEvent()

    Question

  • I have code. It loops in a while as long as bool go = true
    I have a start, and stop button. The stop button is simply go =  !go;
    So, the stop button should halt it. But normally it doesn't. So, I call "Application.DoEvent()" in my loop. This works. Sort of. I have to click the button twice. It seems as though first click deselects first button, and second click selects Stop. This is pissing me off, and I have this prob in a lot of little apps I make. Can anyone help me eliminate this double click?
    Wednesday, February 04, 2009 1:33 AM

Answers

  • We treat the use of Application.DoEvents() as a bug; people are not allowed to check in code that uses it unless they can demonstrate that it fixes a very specific problem. The problem with it is that when you call it, it allows reenterant calls to things like menu and button handlers, which results in nasty and difficult to trace bugs.

    The right way to do background tasks is to use a BackgroundWorker component as Geidrius said, or a timer as John suggested.

    Just to reiterate: DO NOT USE Application.DoEvents()!
    • Marked as answer by thepcdude Thursday, February 05, 2009 1:28 AM
    Wednesday, February 04, 2009 9:47 AM

All replies

  •  Your stop button obviously have a Click event. How do you stop your application? Do you simply interrupt the loop or there is something like this.Close ( ); statement in it. It would be of much help if you posted some code instead of sending us guessing.
    AlexB
    Wednesday, February 04, 2009 1:40 AM
  • Using  Application.DoEvent() is a bad practice. .NET framework has BackgroundWorker class that is very easy to use and much better way to handle long lasting operations without blocking UI thread. May be application is very simple and Application.DoEvent() is enough, but simple applications are always good place to try new concepts :)
    Wednesday, February 04, 2009 8:27 AM
  • If you don't need threading to run a non CPU intensive task, this is from the VB6 world of single core processors: 

      Private Sub StartStopButton_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles StartStopButton.Click  
        If StartStopButton.Text = "Start" Then 
          StartStopButton.Text = "Stop" 
          Timer1.start()  
        Else 
          StartStopButton.Text = "Start" 
        End If 
      End Sub 
      Private Sub Timer1_Tick(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Timer1.Tick  
        Timer1.Stop()  
        If StartStopButton.Text = "Start" Then Return 
        'Do stuff.  
        Timer1.Start()  
      End Sub 
    Wednesday, February 04, 2009 8:47 AM
  • We treat the use of Application.DoEvents() as a bug; people are not allowed to check in code that uses it unless they can demonstrate that it fixes a very specific problem. The problem with it is that when you call it, it allows reenterant calls to things like menu and button handlers, which results in nasty and difficult to trace bugs.

    The right way to do background tasks is to use a BackgroundWorker component as Geidrius said, or a timer as John suggested.

    Just to reiterate: DO NOT USE Application.DoEvents()!
    • Marked as answer by thepcdude Thursday, February 05, 2009 1:28 AM
    Wednesday, February 04, 2009 9:47 AM
  • thepcdude said:

    I have code. It loops in a while as long as bool go = true

    //Start Button
    while(go == true)
    {
          //Code
    }

    thepcdude said:

    I have a start, and stop button. The stop button is simply go =  !go;


    //Stop Button
    {
          go = !go;
    }

    Tada! There you are Alex.
    Oh, and thanks everyone who spoke about the background worker. I'll definitely look into that.

    Thursday, February 05, 2009 1:28 AM