I'm outputting two sets of data to a com port with one pushbutton. After sending data set one I want to pause or wait 3 seconds to send data set two. How do I pause the code for 3 seconds?
You can use the Sleep API function, but there's a danger involved. If the user does something in the UI while your Sleep function is idling, the window won't respond. For example, if they click a command button, it won't appear to sink, or if they click a form's minimize button, nothing will happen, until your sleep period expires. This can be confusing to the user.
At a minimum, you should be showing the hourglass cursor while your output code is running. Even better would be to show a "please wait" form or even a progress meter, along with the hourglass.
Rick Sprague
Want the best answers? See faq181-2886 To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
That's a good point RickSpr, You are completely correct that when using Sleep, the application will not respond to any events during the sleep interval.
But I wouldn't necessarily call that a danger of using the Sleep API. In fact, it may be quite desireable not have any user-interactions or other interruptions during the Sleep interval depending on what your program is doing during that interval. If for example, a second data set can be sent by the pressing of a button, then using sleep will prevent that button event from firing during the interval, whereas the DoEvents loop will not prevent the firing during the interval.
I'm glad that you brought it up, because one does need to know the ramifications of their choices, be it not having any user interaction during the sleep interval, or the performance implication of using a tight DoEvents loop. Both have their places, and as you pointed out, they are not interchangeable.
Good Luck
-------------- As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.