user-0 提问: mfc消息机制

回复:
user: user-1
content: 个人意见：不要轻易打开“多线程”这个‘潘多拉盒子’，尤其是使用mfc时。  否则必须： 《Windows核心编程》 Synchronization Functions The following functions are used in synchronization.   CancelWaitableTimer   CreateEvent   CreateMutex   CreateSemaphore   CreateWaitableTimer   DeleteCriticalSection   EnterCriticalSection   GetOverlappedResult   InitializeCriticalSection   InitializeCriticalSectionAndSpinCount   InterlockedCompareExchange   InterlockedDecrement   InterlockedExchange   InterlockedExchangeAdd   InterlockedIncrement   LeaveCriticalSection   MsgWaitForMultipleObjects   MsgWaitForMultipleObjectsEx   OpenEvent   OpenMutex   OpenSemaphore   OpenWaitableTimer   PulseEvent   QueueUserAPC   ReleaseMutex   ReleaseSemaphore   ResetEvent   SetCriticalSectionSpinCount   SetEvent   SetWaitableTimer   SignalObjectAndWait   TimerAPCProc   TryEnterCriticalSection   WaitForMultipleObjects   WaitForMultipleObjectsEx   WaitForSingleObject   WaitForSingleObjectEx

user: user-2
content: 引用 5 楼 user-3 的回复:简单的说，所有的代码都在某个线程里执行，如果你没开新线程，那么所有的代码实际上都是在一个线程（也叫主线程）里执行 包括你写的所有功能代码，包括不是你写的所有界面绘制代码 sleep(3000)的功能是“当前线程停止执行3秒钟”，这3秒钟内，这个线程什么都不能做，如果你的程序只有一个线程，那就是整个程序在3秒内停止运行  赵老师给出的方法是用循环的方式取代sleep，在循环的时间内处理消息列表的所有消息，也就是这个线程应该执行的所有功能，以此避免当你延时时，整个程序的所有功能都被延迟 这个方法只是权宜之计，根本的解决方法还是多线程   分析的挺好的

user: user-3
content: 简单的说，所有的代码都在某个线程里执行，如果你没开新线程，那么所有的代码实际上都是在一个线程（也叫主线程）里执行 包括你写的所有功能代码，包括不是你写的所有界面绘制代码 sleep(3000)的功能是“当前线程停止执行3秒钟”，这3秒钟内，这个线程什么都不能做，如果你的程序只有一个线程，那就是整个程序在3秒内停止运行  赵老师给出的方法是用循环的方式取代sleep，在循环的时间内处理消息列表的所有消息，也就是这个线程应该执行的所有功能，以此避免当你延时时，整个程序的所有功能都被延迟 这个方法只是权宜之计，根本的解决方法还是多线程

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:将Sleep(3000);替换为 MSG msg; for (int i=0;i<30;i++) {         Sleep(100);         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         } }   赵老师这是用于操作键盘、鼠标消息的吧！ 我这里相当于给某个  sockfd  注册个消息。有个回调会去负责有可读信息时recv。  我做了个实验。一个按钮对于的函数里创建socket，必须要这个函数返回后回调函数才会执行。 不管在函数内等待多久回调函数都不会执行，就算有信号过来。

user: user-0
content: CString str1, str2, str3, str3;       m_message.SetWindowTextA("\r\nnihao.");       Sleep(1000);        m_message.GetWindowTextA(str1);       str2.Format("\r\n%s"," wo hen hao");       str2 = str1 + str2;       m_message.SetWindowTextA(str2);       Sleep(1000);       m_message.GetWindowTextA(str3);       str4.Format("\r\n%s","na jiu hao");       str4 = str3 + str4;       m_message.SetWindowTextA(str4    就是先显示:        nihao 停顿一秒，再显示：     nihao    wohenhao 停顿一秒，再显示：    nihao    wohenhao    najiuhao  现在就只能看到最后的结果，中间的没有。。用Updatedate(FALSE)没有效果！！！

user: user-0
content: 有个edit控件。ID是IDC_MESSAGE 对于的变量名是 m_message;  怎么让它分层显示。。好像界面等到执行完了显示一次。。   CString str1, str2, str3, str3; m_message.SetWindowTextA("\r\nnihao."); Sleep(1000); m_message.GetWindowTextA(str1); str2.Format("%s", wo hen hao); str2 = str1 + str2; m_message.SetWindowTextA(str2); Sleep(1000); m_message.GetWindowTextA(str3); str4.Format("%s",na jiu hao); str4 = str3 + str4; m_message.SetWindowTextA(str4 );

user: user-1
content: 引用 3 楼 u012889234 的回复:          CString str1, str2, str3, str3;       m_message.SetWindowTextA("\r\nnihao.");       Sleep(1000);        m_message.GetWindowTextA(str1);       str2.Format("\r\n%s"," wo hen hao");       str2 = str1 + str2;       m_message.SetWindowTextA(str2);       Sleep(1000);       m_message.GetWindowTextA(str3);       str4.Format("\r\n%s","na jiu hao");       str4 = str3 + str4;       m_message.SetWindowTextA(str4    就是先显示:        nihao 停顿一秒，再显示：     nihao    wohenhao 停顿一秒，再显示：    nihao    wohenhao    najiuhao  现在就只能看到最后的结果，中间的没有。。用Updatedate(FALSE)没有效果！！！ InvalidateRect The InvalidateRect function adds a rectangle to the specified window's update region. The update region represents the portion of the window's client area that must be redrawn.   BOOL InvalidateRect(   HWND hWnd,  // handle of window with changed update region   CONST RECT *lpRect,               // address of rectangle coordinates   BOOL bErase // erase-background flag );   Parameters hWnd  Handle to the window whose update region has changed. If this parameter is NULL, the system invalidates and redraws all windows, and sends theWM_ERASEBKGND and WM_NCPAINT messages to the window procedure before the function returns.  lpRect  Pointer to a RECT structure that contains the client coordinates of the rectangle to be added to the update region. If this parameter is NULL, the entire client area is added to the update region.  bErase  Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged.  Return Values If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero.   Windows NT: To get extended error information, callGetLastError.  Remarks The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.   The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window.   If the bErase parameter is TRUE for any part of the update region, the background is erased in the entire region, not just in the given part.   Windows CE: The hWnd parameter cannot be NULL.  QuickInfo   Windows NT: Requires version 3.1 or later.   Windows: Requires Windows 95 or later.   Windows CE: Requires version 1.0 or later.   Header: Declared in winuser.h.   Import Library: Use user32.lib.  See Also Painting and Drawing Overview, Painting and Drawing Functions, BeginPaint, InvalidateRgn, RECT, ValidateRect, ValidateRgn,WM_ERASEBKGND, WM_NCPAINT, WM_PAINT

user: user-0
content: 引用 7 楼 zhao4zhong1 的回复:个人意见：不要轻易打开“多线程”这个‘潘多拉盒子’，尤其是使用mfc时。  否则必须： 《Windows核心编程》 Synchronization Functions The following functions are used in synchronization.   CancelWaitableTimer   CreateEvent   CreateMutex   CreateSemaphore   CreateWaitableTimer   DeleteCriticalSection   EnterCriticalSection   GetOverlappedResult   InitializeCriticalSection   InitializeCriticalSectionAndSpinCount   InterlockedCompareExchange   InterlockedDecrement   InterlockedExchange   InterlockedExchangeAdd   InterlockedIncrement   LeaveCriticalSection   MsgWaitForMultipleObjects   MsgWaitForMultipleObjectsEx   OpenEvent   OpenMutex   OpenSemaphore   OpenWaitableTimer   PulseEvent   QueueUserAPC   ReleaseMutex   ReleaseSemaphore   ResetEvent   SetCriticalSectionSpinCount   SetEvent   SetWaitableTimer   SignalObjectAndWait   TimerAPCProc   TryEnterCriticalSection   WaitForMultipleObjects   WaitForMultipleObjectsEx   WaitForSingleObject   WaitForSingleObjectEx  这太复杂了，我现在也只是临时需要才先学的，，，， 刚说的哪个方法我试了。没有问题。。  我的那个edit显式的怎么解决啊？？

user: user-1
content: 将Sleep(3000);替换为 MSG msg; for (int i=0;i<30;i++) {         Sleep(100);         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         } }