Computing.Net > Forums > Programming > VC, help for thread programming

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VC, help for thread programming

Reply to Message Icon

Original Message
Name: Quanlh
Date: October 7, 2001 at 09:03:53 Pacific
Subject: VC, help for thread programming
Comment:

I create a MFC (exe) in VC++ 6 with
name ThreadExample. In CThreadExampleDlg,
I have two function below :

DWORD WINAPI CThreadExampleDlg::ThreadFunc( LPVOID lpParam )
{

MessageBox("Thread created.", MB_OK );

return 0;
}

void CThreadExampleDlg::RunEx()
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;

hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier

// Check the return value for success.

if (hThread == NULL)
MessageBox( "CreateThread failed." );

CloseHandle( hThread );
}

It generate an error below :
Cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'. None of functions with this name in scope match the target.

I had use some way consist cast type ThreadFunc to
LPTHREAD_START_ROUTINE) ThreadFunc but it continue generate error.

How can I fix this problem ?


Report Offensive Message For Removal


Response Number 1
Name: Mik
Date: October 8, 2001 at 00:02:05 Pacific
Reply: (edit)

Well you need to leave the cast to (LPTHREAD_START_ROUTINE). But it's giving this error because the thread routine is a member of a class. Try making it a function that is not part of any class.

Mik


Report Offensive Follow Up For Removal

Response Number 2
Name: Quanlh
Date: October 9, 2001 at 00:09:44 Pacific
Reply: (edit)

Thank for your advise, Mik.

But I want to build my application on Dialog base, so it must be derived from CDialog.

Can you tell me another way to create thread as a part of class ?


Report Offensive Follow Up For Removal

Response Number 3
Name: Yogesh
Date: October 28, 2001 at 22:40:32 Pacific
Reply: (edit)

beginthread is another way of creating thread.. try using that..
but i think even if u use threadfunc as separate function.. it will solve the purpose


Report Offensive Follow Up For Removal

Response Number 4
Name: keetha
Date: March 10, 2002 at 12:24:09 Pacific
Reply: (edit)

One must use static member functions in the class and pass "this" pointer. you can call the function as C++ callback only if the function is static member.

see Q102352 in msdn.


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software