CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2009
    Posts
    5

    Program hangs with Doevents

    The problem I've noticed is if a sub with DoEvents is running and you click and hold on the top of the application window like you would if you want to move the form to the other side of the screen, the application stops and hangs until you release it. It does the same thing if you click and hold on the forms min/max buttons. I assume that this is because the event triggered hasn't completed yet so I haven't returned back from DoEvents.

    So in my example below, if you run this and click and hold the form and drag it around the screen, you will notice that the "counter" stops until you release the form. Any ideas how to get around this to keep my program from hanging?


    Code:
    Dim stoprequest As Boolean


    Private Sub Command1_Click()
    Dim counter As Long

    stoprequest = False
    While stoprequest = False
    DoEvents
    counter = counter + 1
    Label1.Caption = counter
    Wend

    End Sub

    Private Sub Command2_Click()
    stoprequest = True
    End Sub

    Private Sub Form_Load()
    stoprequest = True
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    End
    End Sub

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Re: Program hangs with Doevents

    As a wise man once said "Why would you want to do this ?"

    It appears the VB6 Runtime stops when you move a screen

    I guess the problem is there are litterally thousands of events which the run time checks for (on each and every control on the screen)

    eg, Mouse move, mouse down, mouse over, clicks, double clicks, drag etc etc

    So I guess it just says "I give up" until you put the screen down again

    See the screen as a bucket

    Now who in their right mind would want to be filling the bucket with more water while you are moving it across the room ??

    I rest my case !!!

    I am sure that will leave you

  3. #3
    Join Date
    Aug 2000
    Location
    Essex, Uk
    Posts
    1,214

    Re: Program hangs with Doevents

    Move up to .NET and use Threading
    If you find my answers helpful, dont forget to rate me

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program hangs with Doevents

    Silverlight!

    We put a MOVE UP and MOVE RIGHT command into the CLICK_EVENT of a control that just looped around a circle.

    As soon as you tried to click it, it moved away as soon as you tried to click it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: Program hangs with Doevents

    you can also try to put sleep command within the loop to ease the CPU load

  6. #6
    Join Date
    Mar 2009
    Posts
    5

    Re: Program hangs with Doevents

    Thanks everyone for the suggestions. Let me give you a little more background:

    I'm working on a small application that needs to have accurate timing resolution (+/-5mSec). I am using timeGetTime from the windows multi media dll library. Once the user starts the program, I sit in a sub comparing current time versus elapsed time. Once I reach my desired timeout, I execute a call to another sub, perform some tasks, refresh a listbox and return back to wait for another timeout. While waiting for the timeout (10-20mSec) I run DoEvents. I stay in the sub waiting for the timeout until the user hits the stop button. Basically my program is sending timed messages on an external data bus, the message have to go at regular intervals. Everything works fine until the user moves the form window or they use one of my pull down menus. Once they do that, the messages until they release.

    I started looking at .Net, at least with this I can take advantage of background threads.

  7. #7
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Program hangs with Doevents

    I actually made a solution with threading in VB6, but they are known to crash. Worked great in debug mode though. lol

    Tried API's:
    CreateThread
    SHCreateThread
    Last edited by TT(n); March 27th, 2009 at 08:44 AM.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program hangs with Doevents

    In other words... VB.Net might be closer to the answer you need.

    Of course, less than 15ms is probably going to be a problem, unless you use an external clock and trigger
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Program hangs with Doevents

    Quote Originally Posted by clay1 View Post
    ...I'm working on a small application that needs to have accurate timing resolution (+/-5mSec). I am using timeGetTime from the windows multi media dll library. Once the user starts the program, I sit in a sub comparing current time versus elapsed time. Once I reach my desired timeout, I execute a call to another sub, perform some tasks, refresh a listbox and return back to wait for another timeout. While waiting for the timeout (10-20mSec)
    I wonder about this small timeout because human reaction normally is longer then 100 ms so nobody can hit a key exactly in one of this timeout periods. While this may not matter as the keystroke or mouse move event simple appears in another one of this periods, you may have troubles with the timer itself. Most timers are not such precise as you want. The other thing is that as long as you have one thread all messages are going to a message queue which then will proceed one after the other. Result: You cannot solve your probem as long as you only have one thread running, because anything the user does also is going into this queue of things to be proceeded and this will delay or stop your timer, because your program will come back to your time-comparing-routine- much to late.
    So the only solution is - dont use VB6 for this. Use any .net language, where you can do multithreading. As you said yourself
    at .Net, at least with this I can take advantage of background threads.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Mar 2009
    Posts
    5

    Re: Program hangs with Doevents

    For the most part the timer I'm using has enough resolution. As long as the program keeps responding everythings OK. I did notice this morning that in my example, if you change the forms Movable property to False, the program(timer) doesn't stop. However, if you right click on the title bar to get the move/close popup, the timer stops. I know it's not really the time that stops, for whatever reason, there is an event that doesn't return back from Doevents until the popup closes.

    This is really the point of my example program. To show that there are some event/s that don't return back from Doevents until the user finishes. What I need is a DoSomeEvents function that would only do events I specify. All I'm trying to do is keep the timer running no matter what the user does. This is really more of an annoyance that anything else.

    One more thing I can say, a callback timer works just fine no timer/program hangs. Assuming the resolution is acceptable, I'm going to try a callback timer that has a faster timeout, inside this I'll check timeGetTime for my accurate 10milli second timeout.

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Program hangs with Doevents

    If you use VB.Net, you can create different THREADS that allow background processing as well
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Program hangs with Doevents

    Quote Originally Posted by clay1 View Post
    ... All I'm trying to do is keep the timer running no matter what the user does. This is really more of an annoyance that anything else.....
    Thats exactly what cannot be done. No one can promise that. You simple need to understand some simple words like singlethreaded and Do-Events and you will understand that this allows to complete the next event in the queue of events. If the next event needs longer then your 15 ms then you cannot get back to the originals early enough. You even dont know how many of callback events in between have accumulated on the queue so they, maybe after a 'long' lasting DoEvents action, will be done one after the other. Its like a car jam on the highway. Even when the cars 1000 m behind the jam are coming one after the other each one 50 m distance from the next, they come out of the jam one after the other 3m Distance each.
    So your callbacks will come to the queue every 15 msec, but are they processed in the same way ? YOU CANNOT GRANT THIS. NEVER.
    If you get in one delay, all others will have to wait and then they process in the same speed as they are in the queue. One after the other no timelag in between them. Its up to the receiver program to be able to receive packeges independent if they have different timelags. The timeout for such an action is normally in the range of seconds not 5 msec.
    And you even cannot grant this when a user opens a modal Dialog and doesn't close it 3 minutes and this event waits for finishing and doesn't come back before. There is no secure way then multithreading with at least one separate thread.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  13. #13
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Program hangs with Doevents

    I was playing around with the idea a little more, and remembered that you can make another application(a thread) that does just the counting.
    I used the DrawText API, as a label for updating on the first application.
    All that's left, is to hook up the buttons, so that they communicate with the second application, to start/stop the counter.
    You'd also want to hide the second app.
    The first app is free to update it's label(Drawn by app2) no matter what the user is doing to the first app.

    API's
    GetDC
    DrawText
    FindWindowEx
    PostMessage or SendMessage

    Much easier to just use .NET.
    If that's not an option, then give it a try.

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

Featured