Click to See Complete Forum and Search --> : Event handling in multithreading


damma
May 15th, 2006, 09:53 AM
I have a worker thread which needs to raise an event before terminating. I am using CWinThread to create the thread. I have a EntServerInfo Class from where I am creating the worker thread.
Can anyone give any idea on how to implement the events for the worker thread?

kirants
May 15th, 2006, 08:12 PM
What is the kind of event you want to signal ? Also, what do you mean by implement the events for the worker thread ?

damma
May 16th, 2006, 12:41 AM
thanks for replying.

The worker thread needs to raise as event on completion.

I have the following class:

Class EntServers
{
//code
void Refresh();
void Display();
void GetEntServerInfo(long ServerIdx)
//code
};

void EntServers::Refresh()
{
//Create the thread on refresh
CWinThread *EntServersThread = AfxBeginThread (EntServerInfo, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
HANDLE ThreadHandle = EntServersThread->m_hThread;
EntServersThread->ResumeThread();
}

//This is the proc the thread will execute

UINT EntServerInfo (LPVOID pParam)
{
EntServers *p1 = (EntServers *)pParam;
p1->GetEntServerInfo(-1);

//Here I need to raise an event that the task is done, say EVENT_DONE
return 0;
}

//Handle the event raised by the thread i.e. EVENT_DONE
//On EVENT_DONE I want to call the function EntServers::Display().
Can you tell me how can this be done ?

kirants
May 17th, 2006, 12:09 AM
Question: How do you want the Display() to be done ? Is it supposed to be queued i.e. can it so happen that the EVENT_DONE processing is happening well after the thread EntServerInfo has exited ? or, can it also be done in such a way that the Display happens before EntServerInfo exits ?
If the latter is ok, you can as well do something like this:


UINT EntServerInfo (LPVOID pParam)
{
EntServers *p1 = (EntServers *)pParam;
p1->GetEntServerInfo(-1);

//Here I need to raise an event that the task is done, say EVENT_DONE

//since on EVENT_DONE essentially does a Display(), why not call it here..
p1->Display();

return 0;
}


If the above is not ok, you have to implement some other mechanism of signalling ( which can include using kernel objects for signalling, posting window messages, and others )