Click to See Complete Forum and Search --> : Wait to finish


nbee
April 27th, 2004, 09:40 AM
I have this function:

bool CVirtualDevice::SynchronizeDevice(HANDLE hEvtHandle, long lTimeOutSeconds)
{
MSG Msg;
bool bRst = true;

// '2004.03.18 Seo, B.S. Add TimeOut.
time_t tBegin;
time_t tCurrent;

//if (hEvtHandle == NULL)
// hEvtHandle = m_hSyncEvent;

if (lTimeOutSeconds >= 0)
tBegin = time(NULL);

while(::WaitForSingleObject(hEvtHandle,0) == WAIT_TIMEOUT)
{
if (lTimeOutSeconds >= 0)
{
tCurrent = time(NULL);

if ((tCurrent-tBegin) >= lTimeOutSeconds)
{
bRst = false;
g_Glob->SSG_LOG(LOG_PSYCHE ,LOG_NORNAL,LOG_TRACE, "TimeOut at SynchronizeDevice()");
break;
}
}

if (::PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

Sleep(1);
}

:: CloseHandle(hEvtHandle);

return bRst;
}


and I have 2 functions:

void doOne()
{
SynchronizeDevice(hEvent1, 5);
}

void doTwo()
{
SynchronizeDevice(hEvent1, 10);
}

and I have 2 buttons on the form button1 will call to doOne, button2 will call to doTwo

What I expect is, after 5 seconds, doOne will finish (timeout) and after 10 seconds doTwo will finish (timeout).

But if I click button1 (doOne) and then click button2 (doTwo) -> doOne will freeze until doTwo finish.

Is there any way to separate those 2 loops???

Regards.

MikeAThon
April 27th, 2004, 09:05 PM
The behavior you're seeing is exactly the way you should expect it to work.

When you click doOne, you're basically counting up time in a "while" loop (which never exits naturally since hEvtHandle is never being signalled), pumping one message per iteration. If the message pumped is the button click for doTwo, then you re-enter the SynchronizeDevice function on behalf of doTwo, which must complete before returning control to doOne's earlier invocation of the SynchronizeDevice function.

What are you trying to accomplish? There are ways (such as threads) for de-coupling the doOne and doTwo functions, but it's hard to advise when your goals are unstated.

-Mike

nbee
April 28th, 2004, 12:07 AM
Let's say:

I have 2 functions to do 2 different jobs:

void process1()
{

// Do something...

doOne(hEvent, 10); // 10 seconds timeout.

// Do something again..

}

void process2()
{

// Do something...

doTwo(hEvent, 20); // 20 seconds timeout.

// Do something again..

}

In process1, I need to finish doOne before continue doing the next things inside process1 (similar to process2, doTwo).
If I use my SynchronizeDevice inside doOne and doTwo (as above), I will meet the situation like you said: if I start process1 first, then process2, so the jos under doOne in process1 can not start after 10 seconds timeout. It will wait until doTwo in process2 finish.

So what I exprect is each processs will run separately and the important things is to wait doOne finish to continue in process1, and wait doTwo finish to continue in process2 (I can not figure out how to do this with thread). Is there anyway to enhance SynchronizeDevice function?? If not, is there any solution to solve my case??

Thank you..