Equivalent in C to VB's DoEvents?

Posted on 1998-12-31
Medium Priority
Last Modified: 2010-04-15
I'm a former VB programmer who's moved on to C for sometime know, but as I've been learning how to do things in C that I'd done in Visual Basic, I keep coming across the need for an equivalent to the results of VB's DoEvents function.

I'm working on a program that runs in the background and waits for a program to load. Lets say I'm waiting for Notepad. In VB, I'd make a FindWindow loop, like this:

Dim hwndNotepad As Integer

      hwndNotepad = FindWindow("Notepad", vbNullString)
Loop Until (hwndNotepad <> 0)

Now without that DoEvents line, my program won't be able to accept input while the loop is going. The window won't be able to be resized, no button on it can be clicked, it can't even be closed. DoEvents fixes that. But in C, I'm writing this:

HWND hwndNotepad;

      hwndNotepad = FindWindow("Notepad", NULL);
} while (hwndNotepad == NULL);

That loop will of course lock up the program until Notepad comes along and ends it. But I want to be able to do other things with the program, even quit it, while the loop is going.

Is there an API function that can simply magically take care of this? Or will this require a large complicated restructuring of things? I refuse to believe that a Visual Basic function can't be matched in C.


Chris B.

Question by:DerechaEn
  • 3
  • 2
LVL 11

Accepted Solution

alexo earned 300 total points
ID: 1255518
This is not a C question and you should have asked it in the windows programming area.

There are several solutions to this problem:

1. Do lenghty processing in a secondary thread so the main thread will be free to handle incoming messasges.

2. Create a secondary message loop in another thread.  Take care to call AttachThreadInput() so that message dispatching will work correctly.

3. Do PeekMessage() processing in your lenghty operations.
    See for example: http://support.microsoft.com/support/kb/articles/q74/0/42.asp


Expert Comment

ID: 1255519
I would recommed inserting sleep() followed by PeekMessage() wherever you would normally put doevents.  The doevents function in VB does almost exactly the same thing.  Sleep(0) will relinquish the remainder of the threads timeslice to any other threads that are ready to run.  If no other threads need CPU then your loop will continue. PeekMessage() will check the message que for your message loop.
LVL 11

Expert Comment

ID: 1255520
There is no nees for Sleep() calls.  Win32 uses preemptive multitasking.

Expert Comment

ID: 1255521
Alexo, that is the very reason you use a sleep call.  When called with a parameter of "0", this function relinquishes the remainder of the threads time slice back to the OS if another thread is waiting for processor time.  In a lengthy repetive function, it can make the difference between a smooth user interface and a completely unresponsive one.
LVL 11

Expert Comment

ID: 1255522
heathprovost, I beg to differ.  If you provide for message handling during the processing, there will be no need for "manual" context switches.  However if you have a reference that proves me wrong please provide it.

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question