Help:WaitForSingleObject(sema_id,INFINITE) in MS Visual C++

  • 10 years ago

    Hi guys

    I use WaitForSingleObject(sema_id,INFINITE) in one of my programs (.cpp) compiled by MS Visual C++.  The program is spawned by another program and WaitForSingleObject(..,INFINITE) in it causes a deadlock!! I could have written the program as a dialog based application and use DestroyWindow() in it as a way of clearing the deadlock!!  But the compiler does not recognize DestroyWindow() in the program (C++?) but DestroyWindow(some_handle)!!  How can I get this handle(some_handle)?  I have tried "fclose(stdout), fclose(stdin), fclose(stderr) alternatively but they have made no difference!  Help me!

     

    Thanks

    Chong

  • 10 years ago

    Hi Chong,

    The function of 'WaitForSingleObject' is to wait for an object up to a signal from that object. During the wait, this function locks the thread that it's running in that. Then if you call it in your UI thread, your UI doesn't respond anymore and will cause an 'end task' from task manager. If you want no lock, you should run this function in another thread. But there is another function like this, that doesn't locks current thread, since it runs in its own thread. The function name is 'RegisterWaitForSingleObject'. It has good options, like running a function when the object signaled.

  • 10 years ago

    Thanks again Mohammad for helping me!!  You are a great asset to us all!!!  I will try what you have advise me to do and come back later.

     

    I have found something intersting from the manual though.  The manual mentions that a thread/process which creates windows can cause a system deadlock to itself and others using a wait function with no timeeout interval (i.e.INFINITE).  I have found a trick to get around it in my window-based application.  Since my application (i.e.a dialog based application) does not use the windows at all, I simply call DestroyWindow( ) and continue to perform the rest of the functions with a wait function with INFINITE wait time period.  It seemed to have removed a deadlock!!

    Best regards to Mohammad all the other guys,

    Chong

    P.S.

    How come you know so much Mohammad!!?  You are great!!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Brevity is the soul of wit” - Shakespeare