NO

Author Topic: How to write text to Edit Box from background thread  (Read 2770 times)

rp108

  • Guest
How to write text to Edit Box from background thread
« on: June 28, 2014, 03:56:00 PM »
Hi All,

Pelles c 7.00.355 Win64 on Win7 OS

Project created as Win 64 Dialog

The purpose of my code is to loop count upwards and
update an edit box in a dialog GUI, every 1 sec.


Basically I know that the background thread runs
and counts up because I can use a Dialog Button
to read-to-screen the contents of the global variable.

But I am NOT able to get the background function
to direclty update the Edit Box.

Back when I used MS Vis Stud 6.0 I did this hundreds
of times - you have to use AfxBEginThread().
In Vis Stud 6, MFC it is required that after you
write the data to the Edit Box that you call
UpdateData(FALSE),
but that function gives a error when compiling
as undeclared.



Any help is MUCh appreciated !



======= BEGIN My background function ========

void th01(void* pParam)
{

int jj;

   for (jj = 0; jj < 100; jj++)
   {
   
   //assign jj value to global variable (integer) gx01
   gx01 = jj;

   //convert int to str and set global buffer (char) gbuf1
   sprintf(gbuf1, "%d", gx01);

   //write gbuf1 contents to GUI Edit Box (id = 4003)
   SetDlgItemText(ghwndDlg, 4003, _T(gbuf1));

   //sleep for millisecs
   Sleep(1000);
   }  //  end    for (jj = 0; jj < 100; jj++)

}   // end func void th01(void* pParam)


======= END My background function ========




======= BEGIN code that starts the thread =======

Within the function:  MainDlgProc(),
In the section of "case":  WM_INITDIALOG,
my code is:

   int n = 123;
       _beginthread(&th01, 0, &n);



======= END code that starts the thread =======



======= BEGIN contents of Project Status Box =====

Building C:\BU\Pellus_C_projects\Test02\output\main.obj.
C:\BU\Pellus_C_projects\Test02\main.c(231): warning #2145: Assignment of 'void __fastcall function

(void *)' to 'void __cdecl function(void *)'.
Building C:\BU\Pellus_C_projects\Test02\output\main.res.
Building C:\BU\Pellus_C_projects\Test02\Test02.exe.
Done.

======= END contents of Project Status Box =====


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How to write text to Edit Box from background thread
« Reply #1 on: June 28, 2014, 08:07:46 PM »
It should work as showed in the sample attached.
The function called from _beginthread must be __cdecl or on return will mess up the stack in X32, inX64 doesn't happen because parameters are passed in registers, but something will mess up anyway ...  ::)
'ghwndDlg' must be the dialog window, be sure to assign it correctly:
Code: [Select]
        case WM_INITDIALOG:
ghwndDlg = hwndDlg;
_beginthread(&th01, 0, &n);
            return TRUE;

Be sure that global variables are static defining them outside functions.
« Last Edit: June 28, 2014, 08:09:33 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

rp108

  • Guest
Re: How to write text to Edit Box from background thread
« Reply #2 on: June 28, 2014, 11:47:18 PM »
Hi Frankie,

Once again I am thanking you.

Once again it works like a dream.

I hope that some day I can do something for you :-)

Most of my skills are in hardware design.

All the best,
rp108

P.S. do you do crypto coin mining?  Favorite coin?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How to write text to Edit Box from background thread
« Reply #3 on: June 29, 2014, 12:50:00 PM »
Hi Frankie,

Once again I am thanking you.

You're welcome.

P.S. do you do crypto coin mining?  Favorite coin?
No, I'm very traditional on that!  ;D
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide