Click here to Skip to main content
15,912,082 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: error C2039: 'DebugString' : is not a member of '_DEBUG_EVENT' Pin
ForNow19-Jul-13 5:32
ForNow19-Jul-13 5:32 
QuestionHow can I move CProgressCtrl in CListCtrl? Pin
YannGEF18-Jul-13 22:44
YannGEF18-Jul-13 22:44 
Questiontexture2D stretch Pin
boxfile18-Jul-13 22:29
boxfile18-Jul-13 22:29 
AnswerRe: texture2D stretch Pin
boxfile26-Jul-13 16:40
boxfile26-Jul-13 16:40 
Questionhook game for d3d10 or d3d11 Pin
boxfile18-Jul-13 16:36
boxfile18-Jul-13 16:36 
AnswerRe: hook game for d3d10 or d3d11 Pin
boxfile18-Jul-13 19:47
boxfile18-Jul-13 19:47 
QuestionCalling GUI functions from a worker thread in MFC?? Pin
Kiran Satish18-Jul-13 9:25
Kiran Satish18-Jul-13 9:25 
AnswerRe: Calling GUI functions from a worker thread in MFC?? Pin
pasztorpisti18-Jul-13 11:39
pasztorpisti18-Jul-13 11:39 
Every normal gui framework is single threaded that means: you can access gui related data and functions only from the gui thread (that is the main thread in case of MFC). Its only blind luck that your app worked on WinXP most of the time. Multithreaded programs become quite non-deterministic and the same is true for the occurrence/reproduction of their bugs. Now you are "lucky" that you have a quite reliable repro case for your bug that was a bug even on WinXP!

1. AfxGetMainWnd() is a gui related call so I would call it only from the main/gui thread. Call AfxGetMainWnd() from the gui thread when you create the worker thread and pass the window pointer to the thread as a CreateParam. Since SendMessage() and PostMessage() are thread safe WinAPIs the same is true for the SendMessage() and PostMessage() methods of the window.
2. On the worker thread use the PostMessage() or SendMessage() method on the window pointer you received as a ThreadCreateParam. PostMessage(WM_CLOSE, 0, 0) should work if you use directly the window pointer without AfxGetMainWnd().
3. To call UpdateAllViews() do the following: Define your own window message. Either WM_APP+(0..1000 or whatever) or WM_USER+... Then use again PostMessage() on the window pointer to post your own window message. In the message map of your window receive your user defined message with ON_MESSAGE(WM_MYMESSAGE, OnMyMessageMethod). Your OnMyMessageMethod() will be called from the gui thread so it will be safe to call UpdateAllViews() from there. Use PostMessage() instead of SendMessage() if you don't want the worker thread to block until the end execution of OnMyMessageMethod() on the gui thread.

BTW: Here is the implementation of AfxGetMainWnd() of VS2010:
C++
_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
	{ CWinThread* pThread = AfxGetThread();
		return pThread != NULL ? pThread->GetMainWnd() : NULL; }

If your worker thread was created with non-mfc api then this AfxGetMainWindow() returns NULL always if called from your worker thread.
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
«_Superman_»18-Jul-13 21:25
professional«_Superman_»18-Jul-13 21:25 
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
pasztorpisti18-Jul-13 22:56
pasztorpisti18-Jul-13 22:56 
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
Kiran Satish19-Jul-13 10:46
Kiran Satish19-Jul-13 10:46 
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
pasztorpisti19-Jul-13 14:01
pasztorpisti19-Jul-13 14:01 
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
Kiran Satish19-Jul-13 16:18
Kiran Satish19-Jul-13 16:18 
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
pasztorpisti20-Jul-13 6:07
pasztorpisti20-Jul-13 6:07 
AnswerRe: Calling GUI functions from a worker thread in MFC?? Pin
David Crow19-Jul-13 4:43
David Crow19-Jul-13 4:43 
AnswerRe: Calling GUI functions from a worker thread in MFC?? Pin
Erudite_Eric22-Jul-13 4:59
Erudite_Eric22-Jul-13 4:59 
GeneralRe: Calling GUI functions from a worker thread in MFC?? Pin
Kiran Satish22-Jul-13 5:23
Kiran Satish22-Jul-13 5:23 
QuestionHow to disable copy/paste in CEdit? Pin
Donguy197617-Jul-13 5:53
Donguy197617-Jul-13 5:53 
AnswerRe: How to disable copy/paste in CEdit? Pin
NotPolitcallyCorrect17-Jul-13 6:05
NotPolitcallyCorrect17-Jul-13 6:05 
GeneralRe: How to disable copy/paste in CEdit? Pin
Erudite_Eric17-Jul-13 23:30
Erudite_Eric17-Jul-13 23:30 
GeneralRe: How to disable copy/paste in CEdit? Pin
NotPolitcallyCorrect18-Jul-13 1:40
NotPolitcallyCorrect18-Jul-13 1:40 
AnswerRe: How to disable copy/paste in CEdit? Pin
Erudite_Eric17-Jul-13 23:33
Erudite_Eric17-Jul-13 23:33 
Questionofstream optimization problem Pin
doug2517-Jul-13 4:43
doug2517-Jul-13 4:43 
AnswerRe: ofstream optimization problem Pin
Richard MacCutchan17-Jul-13 4:53
mveRichard MacCutchan17-Jul-13 4:53 
GeneralRe: ofstream optimization problem Pin
doug2517-Jul-13 5:06
doug2517-Jul-13 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.