CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2002
    Location
    Madrid - Spain
    Posts
    78

    Comunication between threads

    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

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Are you referring to a worker threader?

  3. #3
    Join Date
    May 2002
    Location
    Madrid - Spain
    Posts
    78

    Comunication between threads

    Code:
    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

  4. #4
    Join Date
    May 2002
    Posts
    15
    SuspendThread(hThread);
    ResumeThread(hThread);

    These functions allow for remote manupulation of a thread.

    Hope this is what you're looking for!

  5. #5
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147
    You should also look at WaitForSingleObject which describes almost exactly what you want to do.

    Hope this helps,

    - Nigel

  6. #6
    Join Date
    May 2002
    Location
    Madrid - Spain
    Posts
    78

    Comunication between threads

    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?

  7. #7
    Join Date
    Feb 2002
    Posts
    5,757
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured