none
C++ Win Threading to use as a Class

    Question

  • Dear All,
     
    I am trying to write some testing program for multithreading as below.
    Dear All,
     
    I am trying to write some testing program for single threading class as below.
    HANDLE	hStartEvent, hStopEvent = FALSE;
    
    void Thread::StartSingleThread()
    {  
      HRESULT hr = S_OK;  
      / / Start thread 
      hStartEvent = CreateThread(NULL,0,ThreadProc, this,0,0); 
      // Then wait for it to finish before contining 
      DWORD status = WaitForSingleObject (hStartEvent, 600); 
      if (status != WAIT_OBJECT_0)   
      {
          // the thread did not end properly.         
           hr = E_FAIL;
       }
    }
    
    DWORD WINAPI ThreadProc(LPVOID param)
    {  
       int waitout = 40; 
       // Here I would like to exit the starting thread loop if I press or handle event trigger without using Dialog
       while(WaitForSingleObject(hStopEvent,waitout)! =WAIT_OBJECT_0)     
       {
           Sleep(1000);      
        }   return 0; 
    } 
    
    void Thread::StopSingleThread() 
    {
         hStopEvent = CreateEvent(0, TRUE, FALSE, 0);
         //Close the Start Thread     
         //if( NULL != hStopThread ) 
         {       
            // Trigger thread to stop 
            //::SetEvent(hStopEvent); 
            // thread may be suspended, so resume before shutting down 
             //::ResumeThread( hStartThread ); 
            // Wait for the thread to exit. If it doesn't shut down 
             // on its own, force it closed with Terminate thread 
             //if ( WAIT_TIMEOUT == WaitForSingleObject( hStartThread, INFINITE ) ) 
             {
                  //::TerminateThread( hStartThread, -1000 ); 
                  //ExitThread(0);         
             }
          // Close the handle and NULL it out 
          //::CloseHandle( hStartThread );      
          //hStartThread = NULL; 
           }    
        // Reset the shutdown event 
        //::ResetEvent( hStopEvent ); 
    }
    
    <pre>Thread::Thread()
    {
    	// Create events
    	hStopEvent = CreateEvent(0, TRUE, FALSE, 0);
    	bThread = true;
    }
    
    
    Thread::~Thread()
    {
    	// Trigger thread to stop
    	//::SetEvent(hStopThread);	
    
    	// Wait until thread finished
    	//::WaitForSingleObject(hStartThread, INFINITE);	
    
    	// Close handles
    	//::CloseHandle(hStartThread);
    	//::CloseHandle(hStopThread);
    }
    
    I would like to control single thread first then multithreads in win32 by event handle.
    
    However, I couln't manage to stop the thread properly using above code.
    
    I hope someone will advise me and looking forward to hearign from you soon.
    
    

     

    Thursday, July 22, 2010 3:24 AM

Answers

  • Hi Mr Ko Ko,

    Soctt is correct. Normally we create the event before the thread.  You have commented lots of code. Does it mean parts of the code works?

    >>I would like to control single thread first then multithreads in win32 by event handle.

    In Win32, we can handle the message for the windows in message loop. For example we handle WM_CHAR message to use key press “s” to start the thread, and press “t” to stop the thread.  The following code sample is about creating a thread by press “s” and this thread is waiting for a signal which is sent by key press “t”.

    HANDLE hThread;
    
    HANDLE hStopEvent;
    
    int waitout = 40; 
    
    ...
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	switch (message)
    	{
             ......
      case WM_CHAR:
        
        if(wParam == 's'){ // press s
    
          hStopEvent = CreateEvent(0, TRUE, FALSE, 0);// Create a Event
          hThread = CreateThread(NULL,0,threadFunc,NULL,0,NULL); // Create a Thread
        }
       
        if(wParam == 't'){ // press t
          
          SetEvent(hStopEvent); // alter the thread.
       
         // Once Thread is over, close thread handle and event handle.
          while(WaitForSingleObject(hThread,waitout)==WAIT_OBJECT_0)   
          {
            CloseHandle(hThread);// close thread handle
            CloseHandle(hStopEvent);// close Event handle
          } 
           
        }
        
        break;
    
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    DWORD WINAPI threadFunc(LPVOID rParam){
    
    
      while(WaitForSingleObject(hStopEvent,waitout)!=WAIT_OBJECT_0)   
      {
        Sleep(1000);   
      } 
    
    
    MessageBoxA(NULL,"Thread is closed","Test",1);
    return 0;
    
    }
    

    If you have any concern, please let me know.

    Cheers,

    Yi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 23, 2010 10:45 AM
    Moderator

All replies

  • The thread is waiting for hStopEvent but the destructor sets hStopThread.  This seems to be two variables where one is needed.

    The thread handle is hStartEvent but the destructor is waiting for hStartThread.  This seems to be two variables where one is needed.

    hStopEvent is created two places. It must be created before the thread is created so it will be valid when the thread uses it.

    You need to clean this code up.

     

    Thursday, July 22, 2010 1:25 PM
  • Hi Mr Ko Ko,

    Soctt is correct. Normally we create the event before the thread.  You have commented lots of code. Does it mean parts of the code works?

    >>I would like to control single thread first then multithreads in win32 by event handle.

    In Win32, we can handle the message for the windows in message loop. For example we handle WM_CHAR message to use key press “s” to start the thread, and press “t” to stop the thread.  The following code sample is about creating a thread by press “s” and this thread is waiting for a signal which is sent by key press “t”.

    HANDLE hThread;
    
    HANDLE hStopEvent;
    
    int waitout = 40; 
    
    ...
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	switch (message)
    	{
             ......
      case WM_CHAR:
        
        if(wParam == 's'){ // press s
    
          hStopEvent = CreateEvent(0, TRUE, FALSE, 0);// Create a Event
          hThread = CreateThread(NULL,0,threadFunc,NULL,0,NULL); // Create a Thread
        }
       
        if(wParam == 't'){ // press t
          
          SetEvent(hStopEvent); // alter the thread.
       
         // Once Thread is over, close thread handle and event handle.
          while(WaitForSingleObject(hThread,waitout)==WAIT_OBJECT_0)   
          {
            CloseHandle(hThread);// close thread handle
            CloseHandle(hStopEvent);// close Event handle
          } 
           
        }
        
        break;
    
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    DWORD WINAPI threadFunc(LPVOID rParam){
    
    
      while(WaitForSingleObject(hStopEvent,waitout)!=WAIT_OBJECT_0)   
      {
        Sleep(1000);   
      } 
    
    
    MessageBoxA(NULL,"Thread is closed","Test",1);
    return 0;
    
    }
    

    If you have any concern, please let me know.

    Cheers,

    Yi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, July 23, 2010 10:45 AM
    Moderator