Click to See Complete Forum and Search --> : Comunication between threads
sarambur
May 27th, 2002, 12:54 PM
I am developing a program that is loaded at windows startup. At the beggining of my program, i create a thread, that has to wait for a library to be loaded. That library is loaded by windows, and I dont know when exactly is loaded.
So, in the thread, i wait for 30000 milliseconds.
After that, I assume that the library has been loaded by windows, and the thread starts its execution.
I would like to know, how can I pause (from the main program) the thread and how can I resume the execution from the main program too.
I want to know this just in the case, I am able to know when the library I need to be loaded IS loaded by windows.
Thanks in advance,
sarambur
kuphryn
May 27th, 2002, 06:08 PM
Are you referring to a worker threader?
sarambur
May 28th, 2002, 07:04 AM
HANDLE hThread;
BOOL
WINAPI
DllMain(
HINSTANCE hInstance,
DWORD dwReason,
LPVOID lpReserved)
{
dwThrdParam = 1;
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls ( hInstance );
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadEscuchaTarjetaUAX, // thread function
&dwThrdParam,
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThreadTUAX == NULL)
{
MessageBox(NULL,L"CreateThread failed.",L"ERROR",MB_OK);
return(FALSE);
}
break;
case DLL_PROCESS_DETACH:
CloseHandle( hThread );
break;
default:
return(TRUE);
}//Switch
return (TRUE);
}//DllMain
I dont know what is a worker threader, so, please, have a look at the code, and see for yourself.
After the hThread is launched, I want it to pause its execution until some other events happened.
In fact, the ideal execution is:
-The tread starts its execution and waits for a signal/event/notification/ThreadNotification, whatever.
-The main program does what it has to do and tells the thread to continue the execution via a signal/event/notification/ThreadNotification, whatever again.
-The thread receives the signal/event/notification/ThreadNotification and continues the execution.
I mean something like Wait / Notify, but for windows.
Thanks in advance,
Sarambur
Valen
May 28th, 2002, 10:46 AM
SuspendThread(hThread);
ResumeThread(hThread);
These functions allow for remote manupulation of a thread.
Hope this is what you're looking for!
NigelQ
May 28th, 2002, 10:59 AM
You should also look at WaitForSingleObject which describes almost exactly what you want to do.
Hope this helps,
- Nigel
sarambur
May 29th, 2002, 03:45 AM
Hi NigelQ,
I have read the documentation for the WaitForSingleObject, and I dont know what do I have to put as a parameter in that function.
Do you have an example?
I also dont know what are kind of Objects that the WaitForSingleObject procedure can wait for.
Any example?
kuphryn
May 29th, 2002, 09:31 AM
You pass WaifForSingleObject() a handle to your worker thread and a time in millisecond to which you want to wait. For example:
-----
// m_pWorkerThread is a pointer to CWinThread
//
// ThreadFunc is a function you want to call to perform a job
// ptp is a data structure you use to pass data from main thread
// to worker thread. Usually you will want to pass in data.
//
m_pWorkerThread = AfxBeginThread(ThreadFunc, ptp, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
// Disable auto delete after worker thread ends
m_pWorkerThread->m_bAutoDelete = FALSE;
// Get a handle to the worker thread
// You will need this handle later when you want to delete
// the thread.
m_hWorkerThread = m_pWorkerThread->m_hThread;
m_pWorkerThread->ResumeThread();
-----
// Here is one example of the use of WaitForSingleObject
// Here you use it to wait until the worker thread finishes so
// you can delete it
//
-----
ASSERT(m_pWorkerThread != NULL && m_hWorkerThread != NULL);
// Here you pass WaitForSingleObject() a handle to the worker
// thread and a time indicating how long you want to waint.
// INFINITE means you want to wait infinitely
// If you pass in 25, that means you want to wait 25 milliseconds
::WaitForSingleObject(m_hWorkerThread, INFINITE);
delete m_pWorkerThread;
m_pWorkerThread = NULL;
m_hWorkerThread = NULL;
-----
Kuphryn
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.