Passing HWND of edit box to thread?

This is a discussion on Passing HWND of edit box to thread? within the Windows Programming forums, part of the Platform Specific Boards category; I'm having trouble updating a richedit control from a thread. Code: HWND *rawr = new HWND; *rawr = GetDlgItem(hwnd, IDC_MAIN_EDIT); ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    6

    Passing HWND of edit box to thread?

    I'm having trouble updating a richedit control from a thread.

    Code:
                HWND *rawr = new HWND;
                *rawr = GetDlgItem(hwnd, IDC_MAIN_EDIT);
    
                hThread= CreateThread(0, 0, ThreadFunc, rawr, 0,&dwThreadID);
    Code:
    DWORD WINAPI ThreadFunc(LPVOID data)
    {
                HWND *rawr = static_cast<HWND *>(data);
    
                CHARRANGE cr;
                cr.cpMin = -1;
                cr.cpMax = -1;
    
                SendMessage(*rawr, EM_EXSETSEL, 0, (LPARAM)&cr);
                SendMessage(*rawr, EM_REPLACESEL, 0,(LPARAM)"hey look some text");
    
                return 0;
    }
    What's wrong with that code?
    Using dev-cpp on win2k if that matters.

    Thanks,
    Berto

    edit: I suck at the vbcode

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>What's wrong with that code?<<

    Code:
    HWND *rawr = new HWND;
    That is. Get rid of 'new', the HWND* and dereferencing and it should work ok.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    6
    Thanks for the reply! I must admit I didn't even really know what the heck derefrencing was... I tried to read up a bit on the subject, though.. Here's what I have now..


    Code:
    DWORD WINAPI ThreadFunc(LPVOID data)
    {
    HWND rawr = (HWND)data;
    CHARRANGE cr;
    cr.cpMin = -1;
    cr.cpMax = -1;
    
    SendMessage(rawr, EM_EXSETSEL, 0, (LPARAM)&cr);
    SendMessage(rawr, EM_REPLACESEL, 0, (LPARAM)"wow a message");
    }
    Code:
    HWND rawr;
    rawr = GetDlgItem(hwnd, IDC_MAIN_EDIT);
    hThread= CreateThread(0, 0, ThreadFunc, &rawr, 0, &dwThreadID);
    Doesn't crash anymore, but at the same time doesn't add anything to the RichEdit either.. What did I do wrong this time? :-p

    Thanks,
    Berto

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>What did I do wrong this time?<<

    This is:
    Code:
    hThread= CreateThread(0, 0, ThreadFunc, &rawr, 0, &dwThreadID);
    Don't use the address of rawr just use the handle itself ie
    Code:
    hThread= CreateThread(0, 0, ThreadFunc, rawr, 0, &dwThreadID);

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    6
    Thanks again! That makes sense!.. however, now the program just does nothing, and then stops responding. Argh.. any ideas?

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Zip and attach your code, if it's not too big.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    6
    Sure. Here you go.

    Thank you,
    Berto
    Attached Files Attached Files

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Using WaitForSingleObject with a time-out value of INFINITE causes the 'hang' ( it is waiting infinitely long, after all).

    edit: This might be wrong but here goes anyway. The deadlock is caused because WaitForSingleObject with a time-out of INFINITE is waiting for the thead to become signalled which can't happen because your ThreadProc calls SendMessage which 'does not return until the window procedure has processed the message' which can't happen because WaitForSingleObject.....etc. Using PostMessage (which returns immediately) causes the ThreadProc to become signalled so WaitForSingleObject functions as expected (but the control is NOT updated). It appears to work ok if you omit the call to WaitForSingleObject (but this may cause its own problems); attempting to set a non-INFINITE value for the time-out value still results in a non-signalled state for the thread.
    Last edited by Ken Fitlike; 06-07-2003 at 11:41 AM.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    6
    You're my hero.. Any other suggestions as to how I might deal with closing the thread properly, etc?..

    Thanks a -ton-, btw.
    Berto

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message box as thread to stop main thread
    By Yasir_Malik in forum Windows Programming
    Replies: 8
    Last Post: 04-11-2006, 11:07 AM
  2. Capture Enter key press in EDIT Box
    By richiev in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2005, 12:03 AM
  3. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  4. Replies: 2
    Last Post: 05-31-2005, 03:02 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21