Implement a Non-Blocking Delay

Category:
System/API
Type:
Snippets
Difficulty:
Advanced
Author:
Sergey Merzlikin

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
Rather often Visual Basic programmers need to use Windows API functions which delay program execution until the occurence of certain event.

Here is the list of these functions: Sleep, SleepEx, WaitForSingleObject, WaitForSingleObjectEx, WaitForMultipleObjects, WaitForMultipleObjectsEx, MsgWaitForMultipleObjects, MsgWaitForMultipleObjectsEx, SignalObjectAndWait.

So, what problems may encounter the programmer using above API functions with?

Fortunately, there is only one problem, but it is rather serious. The thing is that the programs written on Visual Basic with a small exception are executing in one OS thread. It means that when one of wait functions starts, the "life" of the program completely stops: the visual interface freezes, the buttons became unclickable, and TaskManager reports "Not Responding". More complete discussion of this problem can be found at http://smsoft.chat.ru/en/vbwait.htm

The offered MsgWaitObj function may be used as a non-blocking equivalent of Sleep, WaitForSingleObject and WaitForMultipleObjects functions.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: