Sign in to follow this  
brechtjah

Problem with threads

Recommended Posts

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

Share this post


Link to post
Share on other sites

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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)'

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this