Click to See Complete Forum and Search --> : Creating Thread


lovefool
January 6th, 2005, 01:55 AM
Good day

Example if i have a dialog class CMyDialog, and i hope to start a thread, how should i go about doing it? I try the following but got error.

I have created a function under the CMyDialog...eg

UINT CMyDialog::ThreadFunc(LPVOID pParam)
{
--------
--------
}

Then i use

AfxBeginThread(ThreadFunc,NULL);

I have the following error when compiling.......
"none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)"

Can anyone please advise me how i should go about creating the thread?
Thanks.

lovefool

santoct2002
January 6th, 2005, 02:48 AM
Good day

Example if i have a dialog class CMyDialog, and i hope to start a thread, how should i go about doing it? I try the following but got error.

I have created a function under the CMyDialog...eg

UINT CMyDialog::ThreadFunc(LPVOID pParam)
{
--------
--------
}

Then i use

AfxBeginThread(ThreadFunc,NULL);

I have the following error when compiling.......
"none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)"

Can anyone please advise me how i should go about creating the thread?
Thanks.

lovefool

Usually worker thread functions would be independent from your class . so dont make as class member function.

try the below one.

UINT ThreadFunc(LPVOID pParam)
{
CMyDialog *pObject = (CMyDialog*) (pParam);
*pObject->members
}

Decalre a global function , and if you want to acces class members then pass your object as a parameter.

Refer below code for example.
UINT MyThreadProc( LPVOID pParam )
{
CMyObject* pObject = (CMyObject*)pParam;

if (pObject == NULL ||
!pObject->IsKindOf(RUNTIME_CLASS(CMyObject)))
return 1; // if pObject is not valid

// do something with 'pObject'

return 0; // thread completed successfully
}

// inside a different function in the program
.
.
.
pNewObject = new CMyObject;
AfxBeginThread(MyThreadProc, pNewObject);

lovefool
January 6th, 2005, 03:39 AM
Good Day

Thanks for the info. There is no more errors and the thread do work. However, it only execute the ThreadFunc once. Example

I place the following init

CMyDialog a = new CMyDiglog;
CWinThread* aa = AfxBeginThread(ThreadFunc,a );

in the CMyDialog::OnInitDialog function.

But the thread function, ThreadFunc, seem to execute once only.

How can i make the ThreadFunc routine to execute automatically in interval say once every second or milliseconds? Can i do that?

Please advise. Thanks.

lovefool

Andreas Masur
January 6th, 2005, 03:42 AM
[ Moved thread ]

Andreas Masur
January 6th, 2005, 03:42 AM
Take a look at the following FAQs:

How to create a worker thread? (http://www.codeguru.com/forum/showthread.php?t=312452)
How to end a thread? (http://www.codeguru.com/forum/showthread.php?t=305166)
How to use member functions as thread functions? (http://www.codeguru.com/forum/showthread.php?t=312453)
How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)

santoct2002
January 6th, 2005, 03:51 AM
Use Timer.

Add handler for WM_TIMER in your dialog class and set timer interval as one second. Create thread with in timer function.

lovefool
January 6th, 2005, 04:36 AM
Good Day

Thanks a lot for all the info.

They are very useful.

lovefool

Alin
January 6th, 2005, 04:14 PM
Usually worker thread functions would be independent from your class . so dont make as class member function.


that's not true. there's nothing wrong for a thread to be member of a class. it just needs to be declared as static.

Andreas Masur
January 6th, 2005, 04:19 PM
Usually worker thread functions would be independent from your class.
Well...I would rather say that it is just the opposite if you have designed your system correctly...a workerthread usually works on some kind of data....which should be encapsulated properly within...a class/structure... ;)