Problem with threads

Started by
8 comments, last by brechtjah 15 years, 5 months ago
Little background: I've just started win32, but need to get a function running in another thread to keep my UI untouched. The function looks something like:
void threadje( void *arg)
{
    SetDlgItemInt(hwnd, ID_stcSleep, 10, false);
    Sleep(1000);
}
I call this function in my WinMain function:
_beginthread(threadje, 0, (void*)hwnd);
I've been reading up on threads, however, it is quite hard to find some nice tutorials on what I want. I read somewhere that they just take void * as parameter, and I've copied most of the code found here from a site about threads (http://www.codeproject.com/KB/threads/MultithreadingTutorial.aspx). The error I'm getting is hwnd still being undeclared, I do not understand this as I'm giving it as an argument with the function. Any help would be very well appreciated, thank you
Advertisement
Just because you are passing it doesn't mean that it exists. Make sure that there is something like this somewhere in your code:

HWND hWnd = NULL;

if there isn't, add it in your global variable declarations.

Although you are passing hwnd into your thread, you are not actually using it.
You need to use arg and cast it to the required type, as follows

void threadje( void *arg){    if ( 0 != arg )    {        HWND* hwnd = (HWND *)arg;        SetDlgItemInt(hwnd, ID_stcSleep, 10, false);    }    Sleep(1000);}
Quote:Original post by Googol PL3X
Just because you are passing it doesn't mean that it exists. Make sure that there is something like this somewhere in your code:

HWND hWnd = NULL;

if there isn't, add it in your global variable declarations.


I wouldn't use it as a global. The function may be used by multiple windows, in which case, each process would be picking up the same (wrong) window handle.
Thanks for the quick reactions...
Unfortunately, your code did not work, Googol PL3X.

By using the code of OldProgie2 I'm getting this error:
cannot convert `HWND__**' to `HWND__*' for argument `1' to `BOOL SetDlgItemInt(HWND__*, int, UINT, BOOL)'
HWND* hwnd = (HWND *)arg;

becomes

HWND hwnd = (HWND)arg;

?
Quote:Original post by brechtjah
Thanks for the quick reactions...
Unfortunately, your code did not work, Googol PL3X.

By using the code of OldProgie2 I'm getting this error:
cannot convert `HWND__**' to `HWND__*' for argument `1' to `BOOL SetDlgItemInt(HWND__*, int, UINT, BOOL)'


Sorry, forgot HWND is alread a pointer.

Try

HWND hwnd = (HWND)arg;
void  threadje( void *arg){    while(true) {    HWND hwnd = (HWND)arg;    SetDlgItemInt(hwnd, ID_stcSleep, class.getPlayTime() , false);    Sleep(1000);    }}

Thank you all very much, it works very well now, however, if I may, can I ask why you have to cast it : - )
Since I'm just learning ^^

Thanks
SetDlgItemInt expects a HWND__* and arg is just a void*. You might expect the compiler to just realise that pointers can be cast, but that wouldn't be typesafe - what if the arg that was passed in was a char*, for example?
Oh thanks, I think I get it

This topic is closed to new replies.

Advertisement