Solved

simple multithreading question

Posted on 2002-06-27
19
206 Views
Last Modified: 2013-11-20
I am attempting to write a simple dialog-based application that communicates with other console-based applications over Named Pipes IPC. In my MFC app, I make a call to WaitForMultipleObjects(). Unfortunately, thr call never blocks/waits like it should! I never get the correct return value. In fact, I tested the return value and it is always 'WAIT_FAILED'. Also, the error message associated with GetLastError() is 'The handle is invalid'. Can anyone help? This is very frustrating. Especially when I know that similar code works in several of my console-based apps. As a side note, I tried to get my app to work by waiting on a single object with WaitForSingleObject(). This seemed to work just fine. So, why won't a call to WaitForMultipleObjects() work? Please help!
0
Comment
Question by:rshriner
  • 10
  • 4
  • 4
  • +1
19 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7114874
Could you post some code? Sorry for the inconvenience, but my crystal ball is on repair...
0
 
LVL 32

Expert Comment

by:jhance
ID: 7114890
What that message is saying is that the HANDLE you are passing to WaitForSingleObject() is not a valid handle.  

How are you getting this handle?

WaitForSingleObject() can't wait on something that does not exist.
0
 

Author Comment

by:rshriner
ID: 7114918
Sure. Here's the thread function that is launched from my Dialog's OnInitDialog() function:

void CNptestDlg::npRun()
{
     // DO SOMETHING (this is the MEMBER thread function)

     CString temp;
     DWORD     result;

     HANDLE msgSignal[] = {
          msg[SUBSYSTEM_STATUS].signalingEvent,
          msg[VEHICLE_INFO].signalingEvent,
          msg[WARNING].signalingEvent,
          msg[VIDEO_REQUEST].signalingEvent
     };

    if( ! startMsgThread() )
    {
          return;// -1;
     }

     Sleep(2000);

     while( TRUE )
     {
          result = WaitForMultipleObjects( (sizeof(msgSignal)/sizeof(HANDLE)), msgSignal, FALSE, INFINITE) - WAIT_OBJECT_0;

          if (result == WAIT_FAILED)
               TRACE("Result: (%d) WAIT_FAILED\n", result);

          FormatErrorMsg();

          // Get current time
          //CurrentTime = GetTickCount();

          switch( result )
          {
               case 0:     // SUBSYSTEM_STATUS
                    WaitForSingleObject(msg[SUBSYSTEM_STATUS].lockingMutex, INFINITE);
                    temp.Format("0x92: %X", gv92_Subsystem());
                    ReleaseMutex(msg[SUBSYSTEM_STATUS].lockingMutex);
                    UpdateControl(temp);
                    break;

               case 1:     // VEHICLE_INFO
                    WaitForSingleObject(msg[VEHICLE_INFO].lockingMutex, INFINITE);
                    temp.Format("0x50: %X (speed)", gv50_Speed());
                    ReleaseMutex(msg[VEHICLE_INFO].lockingMutex);
                    UpdateControl(temp);
                    break;

               case 2:     // WARNING
                    WaitForSingleObject(msg[WARNING].lockingMutex, INFINITE);
                    temp.Format("0x51: %X", gv51_Warning());
                    ReleaseMutex(msg[WARNING].lockingMutex);
                    UpdateControl(temp);
                    break;

               case 3:     // VIDEO_REQUEST
                    WaitForSingleObject(msg[VIDEO_REQUEST].lockingMutex, INFINITE);
                    temp.Format("0x20: %X", gv20_VideoRequest());
                    ReleaseMutex(msg[VIDEO_REQUEST].lockingMutex);
                    UpdateControl(temp);
                    break;

               default: // return value is invalid
//                    printf("Wait error: %d\n", GetLastError());
//                    ExitProcess(0);
                    break;
          }
     }

     running = FALSE;
}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rshriner
ID: 7114927
If I wait on any single one of the signalingEvents listed using WaitForSingleObject(), it will work. Does this mean I'll have to create multiple threads that call WaitForSingleObject() for EVERY event I need to wait for?
0
 

Author Comment

by:rshriner
ID: 7114928
If I wait on any single one of the signalingEvents listed using WaitForSingleObject(), it will work. Does this mean I'll have to create multiple threads that call WaitForSingleObject() for EVERY event I need to wait for?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7114947
There we have the problem:

         result = WaitForMultipleObjects( (sizeof(msgSignal)/sizeof(HANDLE)), msgSignal, FALSE, INFINITE) - WAIT_OBJECT_0;

Avoid to substract WAIT_OBJECT_0 from the return value, make it

         result = WaitForMultipleObjects( (sizeof(msgSignal)/sizeof(HANDLE)), msgSignal, FALSE, INFINITE);

        if (result == WAIT_FAILED)
              TRACE("Result: (%d) WAIT_FAILED\n", result);
        else result -= WAIT_OBJECT_0;

BTW, WAIT_FAILED is defined as 0xffffffff...

0
 

Author Comment

by:rshriner
ID: 7114953
Nope. Doesn't appear to work. I wish it did, though. BTW, WAIT_OBJECT_0 is defined as 0.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7114963
OK, so the next obvious question - are you sure *all* of the handles are valid?
0
 

Author Comment

by:rshriner
ID: 7114971
Yes. I tried them individually with WaitForSingleObject(). I'm fairly baffled by this problem... like I said before, I use similar code in several console-based apps and they work just fine...
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 7114976
An 'esoteric' shot in the dark - try to hard-code the number of handles as '4'. Maybe there is some alignment side-effect...
0
 

Author Comment

by:rshriner
ID: 7114981
Tried that, too. What an odd problem...
0
 
LVL 32

Expert Comment

by:jhance
ID: 7114987
Two things from the SDK docs:

1) It may not contain the multiple copies of the same handle.

2) If one of these handles is closed while the wait is still pending, the function's behavior is undefined.

What about #2?
0
 

Author Comment

by:rshriner
ID: 7114995
jhance, I'm a bit confused... what do mean by #2? These handles remain open for the duration of the threads execution...
0
 

Author Comment

by:rshriner
ID: 7115004
jhance, I'm a bit confused... what do mean by #2? These handles remain open for the duration of the threads execution...
0
 
LVL 32

Expert Comment

by:jhance
ID: 7115088
#2 - If one of these handles is closed while the wait is still pending, the function's behavior is undefined.

>>>These handles remain open for the duration of the threads execution

Are you SURE?  How do you know?  It seems to me that for someone who doesn't know where the problem is you sure seem confident about where it ISN'T!!
0
 

Author Comment

by:rshriner
ID: 7115120
OOPS! I just figured it out! I made a call to a startMsgThread() that allocates and initializes the signalingEvents AFTER the populated the msgEvent array. Thus, the msgEvent array elements were all NULL and WaitForMultipleObjects() had nothing to wait on! Boy, I sure wasted some time on this one! Sorry, guys! Hey, is there anyway I can split the points? Give you guys each a few for helping me out? I've only used EE a couple of times and I can't remember how to do something like that (if you even can).
0
 
LVL 32

Expert Comment

by:jhance
ID: 7115160
Glad you found the problem.  Don't you just hate it when the GetLastError() tells you just exactly what the problem is?

POst a 0 pt Question in the Community Support topic area and ask a moderator to slpit things up however you wish.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7118108
I have reduced the points in the question to 50, so you can now finish the split. You should now take the following steps.

1. Post a question in this topic area for either jhance or jkr. Refer to this question in the body of the question, and set the point value at 50.

2. Select the response of the other Expert as the Answer for this question.

3. Post the URL of the new "points for" question in this question.

Netminder
CS Moderator
0
 

Author Comment

by:rshriner
ID: 7121682
Thanks for your help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 140
WinWaitActive parameters 12 31
ffmpeg - "rtsp://...... Operation not permitted" 4 86
Is there a simple front-end menu system. 9 105
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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