Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SetDlgItemText from different thread ?

SetDlgItemText from different thread ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionannouncement
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rmnowick
    wrote on last edited by
    #1

    Hello, I have a dialog box that contains a static text field. The field is just empty and serves as a box that I want to write a number into later. When I use SetDlgItemText I can set the value of the static text field. If I try to call SetDlgItemText from a different thread then I can't seem to get it to work. Anyone ever encounter this problem? The thread I'm trying to call SetDlgItemText from was created by the thread that the dialog box is part of, so I guess it would be a child thread trying to update a static text box in the parents thread. Here is what the code looks like: sprintf(static_message,"%8.3f",measured_value_float); if (chan_num_int == 101) { strcpy(static_message_101,static_message); //SetDlgItemText(hdlg,IDC_STATIC_1,static_message); } if (chan_num_int == 106) { strcpy(static_message_106,static_message); //SetDlgItemText(hdlg,IDC_STATIC_2,static_message); } Right now I'm just moving the values into the variables that start with static_message_10N. The dialog then checks these global variables every second and calls SetDlgItemText itself. I would think that it would be much cleaner if I could just have the other thread do it. Thanks, Robert

    R 1 Reply Last reply
    0
    • R rmnowick

      Hello, I have a dialog box that contains a static text field. The field is just empty and serves as a box that I want to write a number into later. When I use SetDlgItemText I can set the value of the static text field. If I try to call SetDlgItemText from a different thread then I can't seem to get it to work. Anyone ever encounter this problem? The thread I'm trying to call SetDlgItemText from was created by the thread that the dialog box is part of, so I guess it would be a child thread trying to update a static text box in the parents thread. Here is what the code looks like: sprintf(static_message,"%8.3f",measured_value_float); if (chan_num_int == 101) { strcpy(static_message_101,static_message); //SetDlgItemText(hdlg,IDC_STATIC_1,static_message); } if (chan_num_int == 106) { strcpy(static_message_106,static_message); //SetDlgItemText(hdlg,IDC_STATIC_2,static_message); } Right now I'm just moving the values into the variables that start with static_message_10N. The dialog then checks these global variables every second and calls SetDlgItemText itself. I would think that it would be much cleaner if I could just have the other thread do it. Thanks, Robert

      R Offline
      R Offline
      rmnowick
      wrote on last edited by
      #2

      I have some additional information. When running the code in debug I found that the value of hdlg in the code above was zero. That explains why the SetDlgItemText was not working. This presumably comes from my main dialog window code. The routine above has an extern HWND hdlg; so as to specify that the hdlg is declared in a different file. When looking through my dialog routine the interesting thing is that there does not appear to be anywhere that the variable hdlg gets set! Here is the code that creates the dialog box in question that doesn't return until the dialog box goes away... return DialogBox(hInstance, // was (HINSTANCE)hInstance MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC) DialogProc); When you look into DialogProc you see the following... BOOL CALLBACK DialogProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { // Only save the first DBG_SIZE received serial message notifications if (t_win_msgs < DBG_SIZE) { GetTimeStampStr(ats, char_ts, char_ts2); strcpy(win_msg_time[t_win_msgs],char_ts2); win_msg[t_win_msgs] = uMsg; strcpy(win_msg_text[t_win_msgs],""); if (uMsg == WM_COMMAND) strcpy(win_msg_text[t_win_msgs],"WM_COMMAND"); if (uMsg == WM_TIMER) strcpy(win_msg_text[t_win_msgs],"WM_TIMER"); if (uMsg == WM_AGILENT) strcpy(win_msg_text[t_win_msgs],"ser agi"); win_hi_wp[t_win_msgs] = HIWORD(wParam); win_lo_wp[t_win_msgs] = LOWORD(wParam); t_win_msgs++; // Total messages } switch (uMsg) { case WM_AGILENT: return 1; case WM_INITDIALOG: //----------------------------INITDIALOG InitDialogProc(hdlg, uMsg, wParam, lParam); return 1; // Serial data processing if done periodically case WM_TIMER: SetDlgItemText(hdlg,IDC_STATIC_1,(LPCSTR)static_message_101); SetDlgItemText(hdlg,IDC_STATIC_2,(LPCSTR)static_message_106); return 1; The hwnd variable is the first parameter passed into DialogProc. It is then passed as the first parameter to InitDialogProc. It is used in there to do a bunch of stuff including naming the window title, etc... But as I mentioned it does not appear to actually get set anywhere that I can see, unless it is somehow done automatically? If so, then why can't I see it in the other thread? The description of the DialogProc routine seems to show that the first parameter (hdlg) is passed in. In my code, this is not getting set prior to the call to DialogBox that has DialogProc as the 4th parameter. When I set a break in Di

      R 1 Reply Last reply
      0
      • R rmnowick

        I have some additional information. When running the code in debug I found that the value of hdlg in the code above was zero. That explains why the SetDlgItemText was not working. This presumably comes from my main dialog window code. The routine above has an extern HWND hdlg; so as to specify that the hdlg is declared in a different file. When looking through my dialog routine the interesting thing is that there does not appear to be anywhere that the variable hdlg gets set! Here is the code that creates the dialog box in question that doesn't return until the dialog box goes away... return DialogBox(hInstance, // was (HINSTANCE)hInstance MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC) DialogProc); When you look into DialogProc you see the following... BOOL CALLBACK DialogProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { // Only save the first DBG_SIZE received serial message notifications if (t_win_msgs < DBG_SIZE) { GetTimeStampStr(ats, char_ts, char_ts2); strcpy(win_msg_time[t_win_msgs],char_ts2); win_msg[t_win_msgs] = uMsg; strcpy(win_msg_text[t_win_msgs],""); if (uMsg == WM_COMMAND) strcpy(win_msg_text[t_win_msgs],"WM_COMMAND"); if (uMsg == WM_TIMER) strcpy(win_msg_text[t_win_msgs],"WM_TIMER"); if (uMsg == WM_AGILENT) strcpy(win_msg_text[t_win_msgs],"ser agi"); win_hi_wp[t_win_msgs] = HIWORD(wParam); win_lo_wp[t_win_msgs] = LOWORD(wParam); t_win_msgs++; // Total messages } switch (uMsg) { case WM_AGILENT: return 1; case WM_INITDIALOG: //----------------------------INITDIALOG InitDialogProc(hdlg, uMsg, wParam, lParam); return 1; // Serial data processing if done periodically case WM_TIMER: SetDlgItemText(hdlg,IDC_STATIC_1,(LPCSTR)static_message_101); SetDlgItemText(hdlg,IDC_STATIC_2,(LPCSTR)static_message_106); return 1; The hwnd variable is the first parameter passed into DialogProc. It is then passed as the first parameter to InitDialogProc. It is used in there to do a bunch of stuff including naming the window title, etc... But as I mentioned it does not appear to actually get set anywhere that I can see, unless it is somehow done automatically? If so, then why can't I see it in the other thread? The description of the DialogProc routine seems to show that the first parameter (hdlg) is passed in. In my code, this is not getting set prior to the call to DialogBox that has DialogProc as the 4th parameter. When I set a break in Di

        R Offline
        R Offline
        rmnowick
        wrote on last edited by
        #3

        Well, I have it working now but I'm still not sure exactly what is going on. This much I can say. The variable "hwnd" within DialogProc only exists when in the DialogProc routine. So, I created another variable called global_hdlg which I set to hdlg every time DialogProc is called. The other thread then declares global_hdlg as extern and passes it into SetDlgItemText. There must be a cleaner way to do this? Robert

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups