CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    [RESOLVED] getting windows of other threads

    hi!

    I have a dialogbased application, and I created another thread. the problem I have is that I cannot use the window (created by the main thread) in the second thread.

    a call like this:
    Code:
    theApp.m_pMainWnd->SetWindowTextW(L"something");
    in the second thread, causes the program to stop responding - I cannot use it anymore.

    Can you please help me?
    Last edited by Feoggou; November 22nd, 2009 at 09:27 AM. Reason: I changed "blocks the program" with "causes the program to stop responding"

  2. #2
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Unhappy Re: getting windows of other threads

    shouldn't my second thread have full access to the main thread? (like, to access freely its CWnd* -s, or HWND -s)

    I tried sending WM_SETTEXT from this thread to another application, and it worked. But when I tried to send to my own application, to the window belonging to the main thread, the program stops responding, and, in Debug mode, I cannot step any more.

    can anynody please tell me what the problem is and how I can solve it?

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: getting windows of other threads

    How are you creating your thread? You should pass the HWND of the main UI window to the thread as it's thread parameter (or part of a structure passed in the thread parameter) then use SendMessage or PostMessage to interact with the main UI window.

  4. #4
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Unhappy Re: getting windows of other threads

    initially it looked like this:
    Code:
    hSendThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SendThreadProc, this, 0, 0);
    but now I've changed it into this:
    Code:
    hSendThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SendThreadProc, this->m_hWnd, 0, 0);
    and still, the same things happens: it stops responding.

    the definition of SendThreadProc:
    Code:
    DWORD SendThreadProc(HWND hWnd)
    {
    	WCHAR the_string[] = L"message";
    	SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)the_string);
            // code here. at the above line it stops responding
    }
    the part of the code in which I create the thread:
    Code:
    void CMyProgramDlg::OnSend()
    {
    	SetDlgItemTextW(IDC_ST_SEND, L"Not Sending");
    
    	//create the thread for sending the data
    	hSendThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SendThreadProc, this->m_hWnd, 0, 0);
    	
    	//while sending, we disable the "Send" and the "Browse" buttons
    	GetDlgItem(IDOK)->EnableWindow(0);
    	GetDlgItem(IDC_BUTTON_BROWSE)->EnableWindow(0);
    
    	WaitForSingleObject(hSendThread, INFINITE);
    	CloseHandle(hSendThread);
            //code here
    }

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: getting windows of other threads

    At the end of you OnSend routine, you are calling WaitForSingleObject() on your thread routine so your parent thread is going to be blocked for the entirety of your threads existance, which is going to be a long time because SendMessage blocks until the parent hWnd processes the message, but it can't because it is waiting for the thread to end. In short, you have a deadlock.

  6. #6
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: getting windows of other threads

    yeah, you're right.

    I solved the problem now. thanks.

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