MFC Worker Thread Question
-
Update data to what ? is it of the dialog? is so where is the pointer to the dialog window ? I hope i answered ur question with questions. :-)
"When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)
-
UINT WorkerThread(LPVOID pParam)
{
CMyDialog *mydlg_ptr = (CMyDialog *)pParam;
while (1==1)
{
mydlg_ptr->UpdateData();
//do something
mydlg_ptr->UpdateData(false);
}
return 0;
}void main()
{
AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL);
}would that make any difference?
-
Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578
Can you elaborate about the updatedata function ? I mean how u are using it, it obviously not a MFC code. You are not clear with the question.
"When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)
-
UINT WorkerThread(LPVOID pParam)
{
CMyDialog *mydlg_ptr = (CMyDialog *)pParam;
while (1==1)
{
mydlg_ptr->UpdateData();
//do something
mydlg_ptr->UpdateData(false);
}
return 0;
}void main()
{
AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL);
}would that make any difference?
-
Can you elaborate about the updatedata function ? I mean how u are using it, it obviously not a MFC code. You are not clear with the question.
"When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)
What I am doing is writing my own scripting language in MFC. It is done in a text box, the program recognizes functions that the user types in the text box, and executes them accordingly. The user might type something that loops repeadedly, in which case it would have to be done in another thread, as to not interfear with the main dialog. I have a value variable on the text box that I am using for functions like find(), mid(), etc... It only works if it has UpdateData() before it and UpdateData(false) after it. This works completely fine when done in the main thread, but I am having problems doing it in a worker thread. I have tried doing what J.B. suggested, but it caused an assertion failure. Any way to do this? -Dev578
-
Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578
You can't (or shouldn't) access MFC objects between threads. The dialog is created and managed by the main thread, accessing the CDialog object from another thread will often fail, as you've seen. This is just how MFC works. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
-
What I am doing is writing my own scripting language in MFC. It is done in a text box, the program recognizes functions that the user types in the text box, and executes them accordingly. The user might type something that loops repeadedly, in which case it would have to be done in another thread, as to not interfear with the main dialog. I have a value variable on the text box that I am using for functions like find(), mid(), etc... It only works if it has UpdateData() before it and UpdateData(false) after it. This works completely fine when done in the main thread, but I am having problems doing it in a worker thread. I have tried doing what J.B. suggested, but it caused an assertion failure. Any way to do this? -Dev578
Did ya check what caused the assertion? Like is it because of invalid m_hWnd, You can do so by clicking on retry at the assertion box. It will take you to the line that caused the assertion. Assertion is mostly called due to null pointers in MFC. Thread will start doing its job the moment you created it in this case... So it is calling update even before the window is created. Instead in the thread do the following check
if(!::IsWindow(pDlg->m_hWnd))
{
return 0;
}
"When death smiles at you, only thing you can do is smile back at it" - Russel Crowe (Gladiator)
-
UINT WorkerThread(LPVOID pThis) { CMYClass *pThisObject = static_cast<CMyClass *>(pThis); while (1==1) { pThisObject->UpdateData();//do something ThisObject->UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, reinterpret_cast<CMYclass*>(this), THREAD_PRIORITY_NORMAL); } AND DEFINITLY THIS SHOULD WORK!!!! I HAD TRIED A LOT OF TIMES...NO ASSTER OR NO ERROR WERE OCCURED!!! cheers Balkrishna Talele
-
You can't (or shouldn't) access MFC objects between threads. The dialog is created and managed by the main thread, accessing the CDialog object from another thread will often fail, as you've seen. This is just how MFC works. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas." -- Buffy
Hello Michel, I have had seen many times, Coders doing this UINT WorkerThread(LPVOID pThis) { CMYClass *pThisObject = static_cast<CMyClass *>(pThis); while (1==1) { pThisObject->UpdateData();//do something ThisObject->UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, reinterpret_cast<CMYclass*>(this), THREAD_PRIORITY_NORMAL); } So is this still bad idea cheers Balkrishna Talele
-
Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578
In MFC you can't access any CWnd derived objects created in one thread from another, well at least not easilly. You will just get lots of ASSERT's in a Debug build and it simply won't work. One way to do what you want is to use PostMessage() to post a WM_USER_xx message you define. Post this from the worker thread to the Dlg. In the dialog code add a MsgHandler for the WM_USER_xx msg and get it to call UpdateData(). This will work a treat. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
UINT WorkerThread(LPVOID pThis) { CMYClass *pThisObject = static_cast<CMyClass *>(pThis); while (1==1) { pThisObject->UpdateData();//do something ThisObject->UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, reinterpret_cast<CMYclass*>(this), THREAD_PRIORITY_NORMAL); } AND DEFINITLY THIS SHOULD WORK!!!! I HAD TRIED A LOT OF TIMES...NO ASSTER OR NO ERROR WERE OCCURED!!! cheers Balkrishna Talele
If pThisObject is derived from CWnd and the object was created in another thread this won't work. See: http://www.codeproject.com/script/comments/forums.asp?msg=745380&forumid=1647#xx745380xx[^] Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
In MFC you can't access any CWnd derived objects created in one thread from another, well at least not easilly. You will just get lots of ASSERT's in a Debug build and it simply won't work. One way to do what you want is to use PostMessage() to post a WM_USER_xx message you define. Post this from the worker thread to the Dlg. In the dialog code add a MsgHandler for the WM_USER_xx msg and get it to call UpdateData(). This will work a treat. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
-
Here is the problem I am having: UINT WorkerThread(LPVOID pParam) { while (1==1) { UpdateData(); //do something UpdateData(false); } return 0; } void main() { AfxBeginThread(WorkerThread, (LPVOID)this, THREAD_PRIORITY_NORMAL); } The UpdateData() function does not work in the Worker Thread. I have researched this quite a bit, and apparantly others were having the same problem. However, no one was able to come up with a straight answer. Anyone know how to use UpdateData() in a worker thread? Any help is appreciated. -Dev578
First off, remove the calls to
UpdateData()
, and use member control variables instead. Second, what you've created is a "worker" thread, or a thread with no message pump. A thread with no message pump should in no way be interacting with the UI. That is what a "UI" thread, or a thread with a message pump, is for. In the UI thread, simply post a message to the primary thread, the one that owns the UI, with what's to be updated.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)