Solved

Equivalent in C to VB's DoEvents?

Posted on 1998-12-31
5
420 Views
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

Do
      DoEvents
      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;

do
{
      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.

Thanks,

Chris B.

0
Comment
Question by:DerechaEn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 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


0
 
LVL 5

Expert Comment

by:heathprovost
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.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1255520
There is no nees for Sleep() calls.  Win32 uses preemptive multitasking.
0
 
LVL 5

Expert Comment

by:heathprovost
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.
0
 
LVL 11

Expert Comment

by:alexo
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 21
UPD maximums on Red Hat 6 125
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 74
Please explain C language error codes ? 5 44
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

726 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