CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: CreateThread

  1. #1
    Join Date
    Oct 2004
    Posts
    38

    CreateThread

    I do not understand why my program blocks when processing a long routine eventhough I use CreateThread...any clue??

    m_hThread = CreateThread( 0,
    0,
    ThreadFunc,
    (LPVOID)Param,
    0,
    &m_dwThreadID);

    The ThreadFunc is accessed but my dialog remains blocked until the end of the process...

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: CreateThread

    Without seeing your full code me may won't find the problem. But use code tags when posting code!
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    Oct 2004
    Posts
    38

    Re: CreateThread

    This is practically the whole thing I do to call this thread...from the main thread I call this funcion (funcA). Then I pass some parameters using Param.

    Code:
    HANDLE ThreadClass::funcA(BYTE node, WORD time) 
    {
       DWORD Param;
       Param =(6<<24)|(node<<16)|(time);
    
       m_hThread = CreateThread (0,
                              0,
                              ThreadFunc,
                              (LPVOID)Param,
                              0,
                              &m_dwThreadID);
    
       if(!m_hThread)
       {
           // Could not create thread		
           return false;
       }
       return m_hThread;              
    }
    
    
    DWORD WINAPI ThreadClass::ThreadFunc(LPVOID pvParam)
    {		
    	BYTE func_nr = ((DWORD)pvParam>>24)&0xFF;
    	ThreadEventFlag = TRUE;
    	MathClass calc;		
    
    	switch((BYTE)func_nr)
    	{
    	   case 0:
    ...continue...
    Last edited by rafraf; May 31st, 2005 at 07:07 AM.

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: CreateThread

    The calling code of where you call funcA might be more interesting. To see what happens after the thread was created.

    Code Tags...
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    Join Date
    Oct 2004
    Posts
    38

    Re: CreateThread

    here it goes
    Code:
    ...
    case ID_TH_SCAN_INJ:
    {
       ThreadClass thread;
       int node = GetDlgItemInt(hDlg, IDC_COMBO_ELMB, NULL, NULL);
       int time = GetDlgItemInt(hDlg, IDC_TH_SCAN_TIME, NULL, NULL); // in milliseconds
    						
       if(!thread.funcA(node, time))
          MessageBox(NULL, "Thread Error (thread.funcA)", "WRONG PARAMETER", MB_ICONEXCLAMATION | MB_OK);
    
       return TRUE;
    }
    break;
    ...
    Last edited by rafraf; May 31st, 2005 at 07:34 AM.

  6. #6
    Join Date
    Oct 2004
    Posts
    38

    Cool Re: CreateThread

    I have found that the problem comes out when I use this code (see below) inside my ThreadFunc.
    g_hElmbDialog = handle of the dialog I call the thread.
    If intead of calling CreateDialog using g_hElmbDialog, I use NULL, it works!!
    boh??

    Code:
    g_hWaitBox = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_WAITBOX), g_hElmbDialog, (DLGPROC)About);
    if(g_hWaitBox != NULL)
       ShowWindow(g_hWaitBox, SW_SHOW);
    else
       MessageBox(g_hElmbDialog, "CreateDialog Returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
    Do you know why??
    Last edited by rafraf; May 31st, 2005 at 07:56 AM.

  7. #7
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: CreateThread

    Quote Originally Posted by rafraf
    I have found that the problem comes out when I use this code (see below) inside my ThreadFunc.
    g_hElmbDialog = handle of the dialog I call the thread.
    If intead of calling CreateDialog using g_hElmbDialog, I use NULL, it works!!
    boh??

    Code:
    g_hWaitBox = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_WAITBOX), g_hElmbDialog, (DLGPROC)About);
    if(g_hWaitBox != NULL)
       ShowWindow(g_hWaitBox, SW_SHOW);
    else
       MessageBox(g_hElmbDialog, "CreateDialog Returned NULL", "Warning!", MB_OK | MB_ICONINFORMATION);
    Do you know why??
    Because if you show a dialog which is the child of another the parent window get's disabled until the child window has finished. This is the same behaviour as you would display a modal dialog > even if you are in another thread the parent window get's blocked until the child window gets closed.

    You see why posting full code?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  8. #8
    Join Date
    Oct 2004
    Posts
    38

    Re: CreateThread

    Yes...That the parent window gets disabled I knew but that it gets blocked is something new for me...

    Thanks for your help!!!

  9. #9
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: CreateThread

    Quote Originally Posted by rafraf
    Yes...That the parent window gets disabled I knew but that it gets blocked is something new for me...

    Thanks for your help!!!
    You are welcome!
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

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