Jamiul alam khan
Jamiul alam khan

Reputation: 169

C++ Multithreading in MFC Dialog Errors

i tried to implement multi threading following the example https://www.tutorialspoint.com/mfc/mfc_multithreading.htm in this link.

UINT CCheckDlg::MyThreadProc(LPVOID Param) {
    while (1){
        Sleep(50); // would do some work here
    }
    return TRUE;
}

this is called by using

AfxBeginThread(MyThreadProc, 0);

the following errors appears

  1. E0304 no instance of overloaded function "AfxBeginThread" matches the argument list
  2. C3867 'CCheckDlg::MyThreadProc': non-standard syntax; use '&' to create a pointer to member

Upvotes: 2

Views: 799

Answers (3)

Landstalker
Landstalker

Reputation: 1368

Here what you must do:

UINT CMFCDlg::MyThreadProc(LPVOID Param) {

    return 1;
}


UINT MyThreadProc(LPVOID pParam)
{  
    return 0;
}


BOOL CMFCDlg::CallingFunction()
{
    // First version 
    CWinThread * pThread = AfxBeginThread(MyThreadProc, 0);

    // Second version 
    pThread = AfxBeginThread(&CMFCDlg::MyThreadProc, 0);

    // third version 
    pThread = AfxBeginThread(MyThreadProc, this, THREAD_PRIORITY_IDLE, 0, CREATE_SUSPENDED);

    //fourth version
    pThread = AfxBeginThread(&CMFCDlg::MyThreadProc, this, THREAD_PRIORITY_IDLE, 0, CREATE_SUSPENDED);

    return TRUE;
}   

Knowing that the function has been declared as a continuation in the class:

UINT static MyThreadProc(LPVOID Param);   

So you have two alternatives: either a global function, or a static function

Upvotes: 0

Adrian Mole
Adrian Mole

Reputation: 51825

There are two problems in your call to AfxBeginThread. First, as pointed out in the comments, you need to ensure that your CCheckDlg::MyThreadProc is a static member function (which must have the __cdecl attribute, although this may be the default), and use &CCheckDlg::MyThreadProc as the first argument.

The other problem is that the 0 (second argument) is being interpreted as an int parameter, and thus the compiler cannot resolve which of the two overloads to select. Use nullptr - or an explicit (void *)(0) - as the second argument, so that the first overload will be selected.

The two overloads for AfxBeginThread are:

CWinThread* AfxBeginThread(
    AFX_THREADPROC pfnThreadProc,
    LPVOID pParam,
    int nPriority = THREAD_PRIORITY_NORMAL,
    UINT nStackSize = 0,
    DWORD dwCreateFlags = 0,
    LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);

CWinThread* AfxBeginThread(
    CRuntimeClass* pThreadClass,
    int nPriority = THREAD_PRIORITY_NORMAL,
    UINT nStackSize = 0,
    DWORD dwCreateFlags = 0,
    LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);

Upvotes: 1

Dong0321
Dong0321

Reputation: 121

Thread proc must be Global function or a Satic member function of Class.

Please check whether the function CCheckDlg::MyThreadProc was defined static or not.

There is a problem in your code as well.

Even though TRUE is evaluated as UINT, this is a problem. Of course it couldn't occur any error. But in general, all Threads returned 0 value when they were terminated successfully, if not they return -1. Maybe TRUE is defined as -1. This means that your thread function always would be terminated as failed.

Upvotes: 1

Related Questions