Creating Worker Thread that exploit Dialog 's Controls.

Discussion in 'MFC' started by KaimTazz, Oct 28, 2007.

  1. KaimTazz

    KaimTazz New Member

    Joined:
    Aug 8, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Hello Everybody.
    I read the following article on www.go4expert.com

    Worker Threads in MFC
    By Sanskruti
    Posted On 28th February, 2007


    Prototype of AfxBeginThread Function

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

    Body of Function

    UINT MyThreadProc(LPVOID pParam)
    {
    if(pParam == NULL)
    AfxEndThread(MY_NULL_POINTER_ERROR);
    char *pStr = (char *) pParam;
    while(*pStr)
    *pStr++ ^= 0xA5;

    return 0;
    }


    I used the Above Given code it works Fine. But what if I want to use any control of my dialog in the function. It simply refuse to work as the following function is not member of the dialog class, hence it can not exploit any control of the dialog.

    If I write this function as a member of my dialog class as follows.

    UINT Myclass :: MyThreadProc(LPVOID pParam)

    then the error occurs,
    None of the 2 overloads can convert parameter1 to UINT(LPVOID).

    Plz help me to write a function with the help of which i can use control(edit control/static control) of my dialog.
    I simply want to display a counter's value in the edit control on the dialog.
    and counter is being incremented in a loop in the function, that is to be used with AfxBeginThread.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Normally for thread what you should be doing is pass the this parameter in the thread function and you can typecast the variable you get into the thread function back to dialog class object and then call the necessary function. Thread function prototype cannot change not the dialog class function's prototype.
     
  3. KaimTazz

    KaimTazz New Member

    Joined:
    Aug 8, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Thank you very much Shabbir, but I have tried this solution already.
    I made a Dialog class's member function that exploits the dialog's control.
    And worte another non-member function for calling this member one.
    The code is as follows.......
    Code:
    void CThreadImplementationDlg::OnBnClickedStart()
    {
    	CWinThread *pThread = AfxBeginThread(MyThread ,(LPVOID)this) ;
    }
    
    UINT MyThread (LPVOID parameter) /*It is a non-member function*/
    {
    CThreadImplementationDlg *handle = static_cast<CThreadImplementationDlg*>(parameter);
    
    	handle->InternalFunc (); /*This calls member function successfully*/
    	return 1;
    }
    void CThreadImplementationDlg ::InternalFunc () 
    /* Member Function that exploits the Dialog's control*/
    {
    	UpdateData();
    	m_label_info = "Thread is Running with count value";
    	UpdateData (FALSE);
    }
    
    When control reaches the InternalFunc(), an assertion error occurs.
    error description is as follows.
    Debug Assertion Failed!
    Program: ThreadImplementation\Debug\ThreadImplementation.exe
    File: Wincore.cpp
    Line: 889
    For information on how your program can cause an assertion failure, see the visual C++ documentation on asserts.


    Now the wincore.cpp at line no. 889 says the following.

    ASSERT((CWnd*)p == this); // must be us

    // Note: if either of the above asserts fire and you are
    // writing a multithreaded application, it is likely that
    // you have passed a C++ object from one thread to another
    // and have used that object in a way that was not intended.
    // (only simple inline wrapper functions should be used)
    //
    // In general, CWnd objects should be passed by HWND from
    // one thread to another. The receiving thread can wrap
    // the HWND with a CWnd object by using CWnd::FromHandle.
    //
    // It is dangerous to pass C++ objects from one thread to
    // another, unless the objects are designed to be used in
    // such a manner.

    Plz help me as what I am doing, does not seems to be legal/valid or a good programming practice.
    Also I am unable to sum up what wincore.cpp is saying as I know a very little about threads.
    Thanks in advance.
     
    Last edited by a moderator: Oct 29, 2007
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Check for the null handle
     
  5. KaimTazz

    KaimTazz New Member

    Joined:
    Aug 8, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Hello Shabbir, Thanks for your instant reply.
    You wrote "Check out NULL HANDLE". I am totally puzzled now that what to make NULL.
    Can you please give me some clear cue.
    BBye.
    :)
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I was just trying to debug your code from my machine sitting here. Check out what is the value in the handle by debugging.
     
  7. Gurjeet Singh Batth

    Gurjeet Singh Batth New Member

    Joined:
    Nov 1, 2007
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Punjab

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice