Creating a thread
-
Following is a funciton where I am calling a thread void CMyView::OnInitialUpdate() { ... AfxBeginThread(Thread_ScanConnectionState, m_sComPort, THREAD_PRIORITY_NORMAL, NULL, NULL, NULL); ... } //Foll is the thread definition UINT CMyView::Thread_ScanConnectionState(LPVOID pParam) { CString sComPort; sComPort = static_cast (pParam); return 1; } The declaration of the thread in the class header of CMyView is as below. UINT Thread_ScanConnectionState( LPVOID pParam ); I get the compilation error as given below error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'UINT (LPVOID)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4105): could be 'CWinThread *AfxBeginThread(AFX_THREADPROC,LPVOID,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4108): or 'CWinThread *AfxBeginThread(CRuntimeClass *,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' while trying to match the argument list '(overloaded-function, CString)' Please let me know the error. laiju
-
Following is a funciton where I am calling a thread void CMyView::OnInitialUpdate() { ... AfxBeginThread(Thread_ScanConnectionState, m_sComPort, THREAD_PRIORITY_NORMAL, NULL, NULL, NULL); ... } //Foll is the thread definition UINT CMyView::Thread_ScanConnectionState(LPVOID pParam) { CString sComPort; sComPort = static_cast (pParam); return 1; } The declaration of the thread in the class header of CMyView is as below. UINT Thread_ScanConnectionState( LPVOID pParam ); I get the compilation error as given below error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'UINT (LPVOID)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4105): could be 'CWinThread *AfxBeginThread(AFX_THREADPROC,LPVOID,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4108): or 'CWinThread *AfxBeginThread(CRuntimeClass *,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' while trying to match the argument list '(overloaded-function, CString)' Please let me know the error. laiju
Hi, First: AfxBeginThread takes a static function pointer. Yours is not a static function. IOW, make Thread_ScanConnectionState static. Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Third: Do not do extra typing: AfxBeginThread's default parameters work fine. You need not pass THREAD_PRIORITY_NORMAL or the NULLs.:) -- ====== Arman
-
Hi, First: AfxBeginThread takes a static function pointer. Yours is not a static function. IOW, make Thread_ScanConnectionState static. Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Third: Do not do extra typing: AfxBeginThread's default parameters work fine. You need not pass THREAD_PRIORITY_NORMAL or the NULLs.:) -- ====== Arman
-
Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Sorry, it seems the editor doesn't love static_cast's signature:):-D:) -- ====== Arman
Arman Z. Sahakyan wrote:
Sorry, it seems the editor doesn't love static_cast's signature
the editor is just html, so don't use html tags...
<>
are interpreted. if you don't want it to, check the Ignore HTML tags in this message (good for code snippets) checkbox or use< >
codes instead...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VisualCalc 3.0] -
Arman Z. Sahakyan wrote:
Sorry, it seems the editor doesn't love static_cast's signature
the editor is just html, so don't use html tags...
<>
are interpreted. if you don't want it to, check the Ignore HTML tags in this message (good for code snippets) checkbox or use< >
codes instead...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VisualCalc 3.0] -
Hi, First: AfxBeginThread takes a static function pointer. Yours is not a static function. IOW, make Thread_ScanConnectionState static. Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Third: Do not do extra typing: AfxBeginThread's default parameters work fine. You need not pass THREAD_PRIORITY_NORMAL or the NULLs.:) -- ====== Arman
-
Thanx for the info. But i didnt know that the THREAD func needs to be static.Thanx anyway laiju
-
Thanx for the info. But i didnt know that the THREAD func needs to be static.Thanx anyway laiju
when a function is a class member (except static ones), it receives an implicit parameter which is the this pointer. this implicit parameter changes the signature of what you see of your function in design mode... for instance :
class A {
public:
void foo(int);
};just considering the case of this function, its signature is in fact :
void A::foo(A*, int);
so, as AfxBeginThread() receives a function pointer, this parameter must implement a particuliar function signature, it must be exactly as you defined it. this is why your function must be global, or at least static (because static members don't know specific instances of a class - they are also known as class function)... hope that helps...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VisualCalc 3.0]