AfxBeginThread
-
Hi, I am new here and I am very impressed with the level of skill I have seen in the articles, so I am very hopefull that someone can tell me where I am going wrong. I have a Dialog based app. in the main dialog header file I have declared a function UINT Recieve(LPVOID pParam); In the cpp file I try to start a worker thread like this AfxBeginThread(Recieve); I get a compiler error error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' The documentation says The controlling function defines the thread. When this function is entered, the thread starts, and when it exits, the thread terminates. This function should have the following prototype: UINT MyControllingFunction( LPVOID pParam ); and my code follows the example exactly. Anyone konow where I am screwing up?
-
Hi, I am new here and I am very impressed with the level of skill I have seen in the articles, so I am very hopefull that someone can tell me where I am going wrong. I have a Dialog based app. in the main dialog header file I have declared a function UINT Recieve(LPVOID pParam); In the cpp file I try to start a worker thread like this AfxBeginThread(Recieve); I get a compiler error error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' The documentation says The controlling function defines the thread. When this function is entered, the thread starts, and when it exits, the thread terminates. This function should have the following prototype: UINT MyControllingFunction( LPVOID pParam ); and my code follows the example exactly. Anyone konow where I am screwing up?
-
Hi, I am new here and I am very impressed with the level of skill I have seen in the articles, so I am very hopefull that someone can tell me where I am going wrong. I have a Dialog based app. in the main dialog header file I have declared a function UINT Recieve(LPVOID pParam); In the cpp file I try to start a worker thread like this AfxBeginThread(Recieve); I get a compiler error error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' The documentation says The controlling function defines the thread. When this function is entered, the thread starts, and when it exits, the thread terminates. This function should have the following prototype: UINT MyControllingFunction( LPVOID pParam ); and my code follows the example exactly. Anyone konow where I am screwing up?
AfxBeginThread requires 2 paramaters, a function pointer and an LPVOID which is usually a pointer to the instance of the class that calls the thread. Also, your thread function should be declared static as well. Then you would spawn the thread like:
AfxBeginThread(Recieve, this);
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
-
Hi, I am new here and I am very impressed with the level of skill I have seen in the articles, so I am very hopefull that someone can tell me where I am going wrong. I have a Dialog based app. in the main dialog header file I have declared a function UINT Recieve(LPVOID pParam); In the cpp file I try to start a worker thread like this AfxBeginThread(Recieve); I get a compiler error error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' The documentation says The controlling function defines the thread. When this function is entered, the thread starts, and when it exits, the thread terminates. This function should have the following prototype: UINT MyControllingFunction( LPVOID pParam ); and my code follows the example exactly. Anyone konow where I am screwing up?
I just tested your code by placing the Recieve function within a clas and get that exact error message. You need to define the function as static if you're going to use a member function as the first parameter of all instance functions is the this pointer. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson -
Does it give the type it is expecting? 9ball wrote: error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' to type ???
No. I copied the error message directly from the output window and put it in the message. I created the app with app wizard. these are the include files #include "afxwin.h" // MFC core and standard components #include "afxext.h" // MFC extensions #include "afxdisp.h" // MFC Automation classes #include "afxdtctl.h" // MFC support for Internet Explorer 4 Common Controls #include "winsock2.h" #include "process.h" I added winsock2.h and process.h, and I have no other classes.
-
No. I copied the error message directly from the output window and put it in the message. I created the app with app wizard. these are the include files #include "afxwin.h" // MFC core and standard components #include "afxext.h" // MFC extensions #include "afxdisp.h" // MFC Automation classes #include "afxdtctl.h" // MFC support for Internet Explorer 4 Common Controls #include "winsock2.h" #include "process.h" I added winsock2.h and process.h, and I have no other classes.
-
I just tested your code by placing the Recieve function within a clas and get that exact error message. You need to define the function as static if you're going to use a member function as the first parameter of all instance functions is the this pointer. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen SigvardssonAlthough I don't totally understand how this works, here is a sample of the code that I'm using. // Declared in the header file.
bool StartReminder(); static UINT ThreadForReminder(LPVOID pParam);
// When a user presses the remind me button I spin my //thread.void CConstantReminderDlg::OnRemindMeButton() { // Semi-interesting code. if(!this->GetThreadIsSpinning()) this->SpinThread(); } }
UINT CConstantReminderDlg::ThreadForReminder(LPVOID pParam) { UINT returnCode = 0; CConstantReminderDlg* pObject = (CConstantReminderDlg*)pParam; if (pObject == NULL) return 1; // if pObject is not valid pObject->StartReminder(); return 0; // thread completed successfully }
void CConstantReminderDlg::SpinThread() { // Set the dialog member to indicate that the // thread is currently spinning. this->SetThreadIsSpinning(true); // Spin the thread. ::AfxBeginThread(ThreadForReminder,this); }
// The user has requested the program begin and the thread // for reminder dialog has been spun.bool CConstantReminderDlg::StartReminder() { CEvent wakeUpTimer(false,false,"probeDroid",NULL); HANDLE hHandle = (HANDLE)wakeUpTimer; COleDateTime timeStart,timeNext; //COleDateTimeSpan spanElapsed; // Indicate that the user wants the program to //continue. this->SetUserWantsToQuit(false); // A bunch of other stuff...
:-D -
Although I don't totally understand how this works, here is a sample of the code that I'm using. // Declared in the header file.
bool StartReminder(); static UINT ThreadForReminder(LPVOID pParam);
// When a user presses the remind me button I spin my //thread.void CConstantReminderDlg::OnRemindMeButton() { // Semi-interesting code. if(!this->GetThreadIsSpinning()) this->SpinThread(); } }
UINT CConstantReminderDlg::ThreadForReminder(LPVOID pParam) { UINT returnCode = 0; CConstantReminderDlg* pObject = (CConstantReminderDlg*)pParam; if (pObject == NULL) return 1; // if pObject is not valid pObject->StartReminder(); return 0; // thread completed successfully }
void CConstantReminderDlg::SpinThread() { // Set the dialog member to indicate that the // thread is currently spinning. this->SetThreadIsSpinning(true); // Spin the thread. ::AfxBeginThread(ThreadForReminder,this); }
// The user has requested the program begin and the thread // for reminder dialog has been spun.bool CConstantReminderDlg::StartReminder() { CEvent wakeUpTimer(false,false,"probeDroid",NULL); HANDLE hHandle = (HANDLE)wakeUpTimer; COleDateTime timeStart,timeNext; //COleDateTimeSpan spanElapsed; // Indicate that the user wants the program to //continue. this->SetUserWantsToQuit(false); // A bunch of other stuff...
:-DSend me your email ([tarcher@mindspring.com](mailto:tarcher@mindspring.com?subject=[CodeProject] AfxBeginThread help)) and I'll send you a very simple example of using AfxBeginThread. You can then compare, discern what you're doing wrong and post what you find here for anyone else that runs into the same problem. Cheers, Tom Archer Inside C#,
Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson