Board index » delphi » Apartment thread hangs

Apartment thread hangs

Hi,
   I have a COM server that creates a thread to do the actual work. The
thread do
something like the following:

    CoInitialize(nil);
    CreateCOmObject(....
    repeat
        MsgWaitForMultipleObjects("More work to do" or "Terminate thread"
...
        if Message then
           begin
           while getMessage(...) do dispatchMessage(...);
           continue;
           end;
        if Terminate then
           break;
       Do Work with com object
    until false;
    CoUninitialize;

Now this thing mostly works. I have  button on the main server form (in the
main thread) that
can set the "Terminate Thread event" --- and the thread quits like a nice
little thread should.
But if i set the events from the OnCLose events of the form and then do a
WaitFor (or WaitForSingleObject) then the whole program (and the machine
sometimes) hangs up!

Any clue, anyone.

Thanks,
Mujahid Beg.

 

Re:Apartment thread hangs


Mujahid Beg <muja...@hypercon.com> wrote in article
<6d1jur$2j...@forums.borland.com>...

Quote
> Hi,
>    I have a COM server that creates a thread to do the actual work. The
> thread do
> something like the following:

>     CoInitialize(nil);
>     CreateCOmObject(....
>     repeat
>         MsgWaitForMultipleObjects("More work to do" or "Terminate thread"
> ...
>         if Message then
>            begin
>            while getMessage(...) do dispatchMessage(...);
>            continue;
>            end;
>         if Terminate then
>            break;
>        Do Work with com object
>     until false;
>     CoUninitialize;

> Now this thing mostly works. I have  button on the main server form (in
the
> main thread) that
> can set the "Terminate Thread event" --- and the thread quits like a nice
> little thread should.
> But if i set the events from the OnCLose events of the form and then do a
> WaitFor (or WaitForSingleObject) then the whole program (and the machine
> sometimes) hangs up!

Are you running NT or 95?  I have run into problems using
WaitForSingleObject on a thread handle in 95.  It would usually never
return from the wait.  The only solution I found was to make the thread
FreeOnTerminate, and have an event that the thread set as the last thing in
Execute.  Then the manager program would wait for that event.  Really ugly
since we should just be able to wait on the thread.  Another possible
problem is if you have an OnTerminate event handler set for the thread.
The thread will try and synchronize before calling it, so if you're main
program is blocked you'll deadlock.

Darren Clark
Architect Engineer
Radiant Systems
dcl...@radiantsystems.com

Re:Apartment thread hangs


Hi Darren,
    I have since solved the problem. The problem is that if you use
WaitForSingleObject and wait on the thread, the the main thread never
processes any messages, and the event handlers never get called.
So instead this works,

while MsgWaitForMultipleobjects(1, ThreadHandle, INFINITE, QS_ALLINPUT) =
WAIT_OBJECT_0+1 do
    Application.ProcessMessages;

Hope this helps,
Mujahid.

Quote
Darren Clark wrote in message <01bd4258$be6139c0$260211ac@hospsfdclark>...
>Are you running NT or 95?  I have run into problems using
>WaitForSingleObject on a thread handle in 95.  It would usually never
>return from the wait.  The only solution I found was to make the thread
>FreeOnTerminate, and have an event that the thread set as the last thing in
>Execute.  Then the manager program would wait for that event.  Really ugly
>since we should just be able to wait on the thread.  Another possible
>problem is if you have an OnTerminate event handler set for the thread.
>The thread will try and synchronize before calling it, so if you're main
>program is blocked you'll deadlock.

>Darren Clark
>Architect Engineer
>Radiant Systems
>dcl...@radiantsystems.com

Other Threads