Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/forums/mfc/)
-   -   Creating Worker Thread that exploit Dialog 's Controls. (http://www.go4expert.com/forums/creating-worker-thread-exploit-dialog-s-t7095/)

KaimTazz 28Oct2007 22:59

Creating Worker Thread that exploit Dialog 's Controls.
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,
UINT nStackSize = 0, DWORD dwCreateFlags = 0,

Body of Function

UINT MyThreadProc(LPVOID pParam)
if(pParam == NULL)
char *pStr = (char *) pParam;
*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.

shabbir 28Oct2007 23:46

Re: Creating Worker Thread that exploit Dialog 's Controls.
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.

KaimTazz 29Oct2007 12:42

Re: Creating Worker Thread that exploit Dialog 's Controls.
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.......

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*/
        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.ex e
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.

shabbir 29Oct2007 17:55

Re: Creating Worker Thread that exploit Dialog 's Controls.
Check for the null handle

KaimTazz 31Oct2007 13:21

Re: Creating Worker Thread that exploit Dialog 's Controls.
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.

shabbir 31Oct2007 17:09

Re: Creating Worker Thread that exploit Dialog 's Controls.
I was just trying to debug your code from my machine sitting here. Check out what is the value in the handle by debugging.

Gurjeet Singh Batth 3Nov2007 11:11

Re: Creating Worker Thread that exploit Dialog 's Controls.
Help Help Help

All times are GMT +5.5. The time now is 05:52.