DoEvents executed when calling handler events manually??
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: DoEvents executed when calling handler events manually??

Hybrid View

  1. #1
    Join Date
    Feb 2003
    Location
    Greece
    Posts
    529

    DoEvents executed when calling handler events manually??

    I have a form with many standard controls on it, buttons, list boxes and a specific list box named List1 and a button Command1.
    When user presses Command1 button, the program runs of course the Command1_Click handler. Also if the user clicks on the List1 box, the program executes List1_Click among other events (mousedown, mouseup etc).

    Now, what is the thing i noticed. Inside the Command1_Click if i put lets say an endless loop 'Do ... blah blah... Loop' (for example), the user cannot click any object on the form because we trapped the instruction pointer in a loop.

    In my code now. I have a Do..Loop making the List1.ListIndex go from down to up.

    X=List1.ListCount-1
    Do While (X>=0)
    List1.ListIndex=X 'causes the List1_Click procedure to run on every selected item
    List1_Click (setting listindex calls already the click event, but i need to run it twice for each item)
    X=X-1
    Loop

    This loop takes about 10 seconds to execute (lots of work done inside the List1_Click handler, simple file operations only nothing strange).

    While the loop is executing, the user can click in the form objects like the program is just waiting and not executing any other code! How come this is possible? And of course if the user clicks on an item in the List1 box, a runtime error appears saying "Still executing last request" !!! and the program terminates. I know we can stop this behavior by disabling/enabling the entire form or specific objects, but...

    So, if calling manually a handler event, automatically (and thats sad) VB runs also some DoEvents (so the user can interruct with the form like nothing happends?).
    - Better live in the digital world -

  2. #2
    Join Date
    Dec 2012
    Posts
    38

    Re: DoEvents executed when calling handler events manually??

    No, VB doesn't put any DoEvents statement itself.

    I think you receive that type of error because the previous operation you were doing on an Item of listbox is not completed yet.

    If you don't want to enable|disable the whole form or the objects, you can add this code to your form:
    Code:
    Private Sub List1_Click()
      Static Locked As Boolean
    
      If Not Locked Then
        Locked = True
        Do While <Expression>
          DoEvents
          <Code>
        Loop
        Locked = False
      End If
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      End           'This forces to end the application even Loop is still executing
    End Sub
    However it is hard to give you an answer without to know WHAT kind of operations you execute in the Loop.
    Last edited by Cereal Killer; December 19th, 2012 at 02:11 AM.

  3. #3
    Join Date
    Feb 2003
    Location
    Greece
    Posts
    529

    Re: DoEvents executed when calling handler events manually??

    The question i made is very much specific. You are offering a solution, which is not the subject of the thread, but thanks anyway.

    I am noticed this: How come in a Do ... Loop with calling a handler event inside it (List1_Click or List1.ListIndex=X) makes VB to let the user interruct with the form. This must not be done and is dangerous for the rest of code. When VB executes a code should freeze the user interruction except if I, the programmer, permit it with a DoEvents statement.
    - Better live in the digital world -

  4. #4
    Join Date
    Dec 2012
    Posts
    38

    Re: DoEvents executed when calling handler events manually??

    A Visual Basic EXE project is a single threaded project. You (or final user) cannot execute two actions at the same time, i.e. you cannot click on a button until the execution of a previous event is finished.

    The only case in which this is possible is when you add to your routine a DoEvents statement. But you have to be careful: if you enter this instruction into a Loop you might fall in a 'return of code'. What this means?

    You have this code:
    Code:
    Private Sub Command1_Click()
      Do While <Expression>
        DoEvents
        <Code>
      Loop
    End Sub
    
    Private Sub cmdQuit_Click()
      Unload Me
    End Sub
    even you can click on cmdQuit button, the form won't be unloaded until Loop finishes. The reason is simple: the DoEvents statement first runs your new event (cmdQuit_Click), and then returns the control on the routine where it was called.

    What you noticed could be this: you might have a DoEvents statement declared somewhere in your code that doesn't let you to have full control on what you're doing.

    If this is the matter the only solutions you have are these:
    1. delete the DoEvents call in the routine where it is;
    2. rewrite your code so it won't conflict with the DoEvents

    PS: I have just a little curiosity, i.e. where are you come from? (your nationality).

  5. #5
    Join Date
    Dec 2001
    Posts
    6,332

    Re: DoEvents executed when calling handler events manually??

    Can you show the code in the List1_Click event? Sometimes it is better to put the code into a sub, and call the sub from the click event, rather than having the code inside the event itself.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: DoEvents executed when calling handler events manually??

    Yep, the problem must be in your code somewhere. VB will only do the doevents if you place a call in the code to allow it to do so.

    Also the code you show says it fires the click event on every selected item but there is no test there to see if the item is selected so it runs the event for all of them.

    Not a good idea to call the click event, would be better to place the code in a separate sub and call that instead, if you need to execute the code when an item is clicked you could call the sub from the click event.
    Always use [code][/code] tags when posting code.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center