Need Help in VC++ Threading
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Need Help in VC++ Threading

  1. #1
    Join Date
    Jul 2011
    Posts
    33

    Question Need Help in VC++ Threading

    Hi,

    I need help regarding VC++ Threading
    Here is my piece of code:

    Code:
    CWinThread* thread=NULL;
    
    thread= AfxBeginThread( LoadData(), this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED  );
    thread->m_bAutoDelete = false;
    thread->ResumeThread();
    
    SUL_WaitForEndOfThread( thread->m_hThread );
    I am getting access violation error in "SUL_WaitForEndOfThread( thread->m_hThread );"
    When my compiler reaches "thread->ResumeThread();" it doesn't call LoadData().
    Instead it moves to the next statement SUL_WaitForEndOfThread( thread->m_hThread ); and throws me a access violation error.

    Need help in this.

    Reagrds

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need Help in VC++ Threading

    1. What is SUL_WaitForEndOfThread?
    2. What is LoadData? Why and how is it supposed to be called?
    3. What exactly gets this "access violation error": the compiler or your application?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2011
    Posts
    33

    Re: Need Help in VC++ Threading

    My SUL_WaitForEndOfThread() contains the following:
    Code:
    void  SUL_WaitForEndOfThread( HANDLE hThread )
    {
    
    	DWORD		appThreadID = AfxGetApp()->m_nThreadID;
    	DWORD		threadID = AfxGetThread()->m_nThreadID;
    	DWORD		result;
    	int count = 20;
    
    
    	if ( appThreadID == threadID )
    	{
    		SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread"), hThread );
    
    		MSG* pMsg = &AfxGetThread()->m_msgCur;
    		count = 100;
    		while ( TRUE )
    		{
    			result = ::WaitForSingleObject( hThread, 0 );
    			if ( count == 100 )
    			{
    				switch ( result )
    				{
    				case WAIT_ABANDONED:
    					SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_ABANDONED - %d"), hThread, result );
    					break;
    				case WAIT_OBJECT_0:
    					SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_OBJECT_0 - %d"), hThread, result );
    					break;
    				case WAIT_FAILED:
    					SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_FAILED - %d"), hThread, result );
    					break;
    				case WAIT_IO_COMPLETION:
    					SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_IO_COMPLETION - %d"), hThread, result );
    					break;
    				case WAIT_TIMEOUT:
    					SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - WAIT_TIMEOUT - %d"), hThread, result );
    					break;
    				default:
    					SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from main thread - %d"), hThread, result );
    					break;
    				}
    				count = 0;
    			}
    			count++;
    			if ( result != WAIT_TIMEOUT )
    			{
    				break;
    			}
    			if ( ::PeekMessage( pMsg, NULL, NULL, NULL, PM_REMOVE ) )
    			{
    				if ( !AfxGetThread()->PreTranslateMessage( pMsg ) )
    				{
    					::TranslateMessage( pMsg );
    					::DispatchMessage( pMsg );
    				}
    			}
    			else
    			{
    				Sleep( 50 );
    			}
    		}
    	}
    	else
    	{
    		SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread"), hThread );
    		while ( ( result = ::WaitForSingleObject( hThread, 50 ) ) == WAIT_TIMEOUT )
    		{
    			if ( count == 20 )
    			{
    				SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_TIMEOUT - %d"), hThread, result );
    				count = 0;
    			}
    			count++;
    			Sleep( 100 );
    		}
    		switch ( result )
    		{
    		case WAIT_ABANDONED:
    			SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_ABANDONED - %d"), hThread );
    			break;
    		case WAIT_OBJECT_0:
    			SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_OBJECT_0 - %d"), hThread );
    			break;
    		case WAIT_FAILED:
    			SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_FAILED - %d"), hThread );
    			break;
    		case WAIT_IO_COMPLETION:
    			SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_IO_COMPLETION - %d"), hThread, result );
    			break;
    		case WAIT_TIMEOUT:
    			SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - WAIT_TIMEOUT - %d"), hThread, result );
    			break;
    		default:
    			SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - from background thread - %d"), hThread, result );
    			break;
    		}
    		//::WaitForSingleObject( hThread, INFINITE );
    	}
    
    	SUL_Log( 5, -1, _T("SUL_WaitForEndOfThread( %d ) - end"), hThread );
    }
    LoadData() is the thread function to be called once a thread is created. It contains the block of code to be executed for some operations.

    The compiler gives me this Access Violation Error.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need Help in VC++ Threading

    Quote Originally Posted by p_zshk View Post
    ...
    The compiler gives me this Access Violation Error.
    In what line of your code?
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2011
    Posts
    33

    Re: Need Help in VC++ Threading

    "while ( ( result = ::WaitForSingleObject( hThread, 50 ) ) == WAIT_TIMEOUT )"

    this line gives me the Acess Violation.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need Help in VC++ Threading

    This your call of AfxBeginThread is wrong:
    Quote Originally Posted by p_zshk View Post
    Hi,
    Here is my piece of code:
    Code:
    thread= AfxBeginThread( LoadData(), this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED  );
    The correct one is
    Code:
    thread= AfxBeginThread( LoadData, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED  );
    Victor Nijegorodov

  7. #7
    Join Date
    Jul 2011
    Posts
    33

    Re: Need Help in VC++ Threading

    oops...done a typo error here...sorry for that...

    i have written the syntax the proper way...
    thread= AfxBeginThread( LoadData, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );

    may be there is some other problem...

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need Help in VC++ Threading

    Show us your LoadData signature
    Victor Nijegorodov

  9. #9
    Join Date
    Jul 2011
    Posts
    33

    Re: Need Help in VC++ Threading

    Here is the declaration:
    Code:
    static UINT LoadData( LPVOID aParam );
    Last edited by p_zshk; September 9th, 2011 at 04:02 AM.

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,975

    Re: Need Help in VC++ Threading

    Actually I can't see any reason to create the thread suspended as long as you resume it immediately. So try to get rid of the stuff and see if your problem went off.
    Best regards,
    Igor

  11. #11
    Join Date
    Jul 2011
    Posts
    33

    Re: Need Help in VC++ Threading

    I am setting m_bAutoDelete to false, that's why I am creating a thread in suspended mode and then starting the thread through ResumeThread().

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need Help in VC++ Threading

    Well, if you don't start a thread in suspended mode (and don't call ResumeThread) does the problem persist?

    If Yes - create a small project reproducing this behaviour and post it (in zip archive) to the Forum.
    Victor Nijegorodov

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,975

    Re: Need Help in VC++ Threading

    I am setting m_bAutoDelete to false, that's why I am creating a thread in suspended mode and then starting the thread through ResumeThread().
    You never need to suspend thread for this.
    m_bAutoDelete

    Specifies whether to destroy the object at thread termination.
    It has nothing to do with the thread, it's just about CWinThread object, and nothing more than this.
    Last edited by Igor Vartanov; September 9th, 2011 at 09:53 AM.
    Best regards,
    Igor

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center