本人初学线程,下面部份代码为: 开启一个线程后向窗口发送消息,为何不响应.......
代码如下:LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) 
{
case WM_INITDIALOG :
CreateThread(NULL,0,Thread,0,0,NULL) ;
return 0 ;
case WM_COMMAND:
MessageBox(hwnd,"dfdf","dfdf",MB_OK) ;
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(hWnd, message, wParam, lParam);
return 0 ;
   }
   return 0;
}//THREAD FUNCTION
DWORD WINAPI Thread(LPVOID  lp)
{
 SendMessage(hwnd,WM_COMMAND,0L,0L) ;  //该发送后不响应
 return 0L ;
};说明: hwnd 为全局变量 .请高手讲解讲解啊................

解决方案 »

  1.   

    If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. However, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. To prevent this, use SendMessageTimeout with SMTO_BLOCK set. For more information on nonqueued messages, see Nonqueued Messages.
      

  2.   

    回复楼上: 用postmessage() 结果一样我的程序结构是这样的:
    一个进程里的主线程里创建一个非模态窗口.
    然后在该窗口的初始化消息中(WM_INITDIALOG) 中创建另一个线程 .
    该线程然后向该窗口里发送一个消息....
      

  3.   

    不响应该是  SendMessage(hwnd,WM_COMMAND,0L,0L);中的wparam设置错误,不应该是0.
      

  4.   

    回复楼上:
    还是不可以,,,,
    现附上全部代码 ,期实很短, 一个.cpp ,然后资源方件中就一个没有任何子窗口的对话框.
    #include "stdafx.h"
    #include "resource.h"
    #define  THREAD  181818
    // Global Variables:
    HINSTANCE hInst; // current instance
    HWND  hwnd ;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
    DWORD WINAPI Thread(LPVOID);
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
      // TODO: Place code here.
    hInst = hInstance ;
    MSG msg;
    hwnd = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_PROCESS),NULL,DLGPROC(WndProc)) ;
    if (!hwnd)
    MessageBox(NULL,"update process is fail ,please call administrator","error prompt",MB_OK) ;
    UpdateWindow(hwnd) ;
    ShowWindow(hwnd,nCmdShow) ;
    // Initialize global strings // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    } return msg.wParam;
    }LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message) 
    {
    case WM_INITDIALOG :
    CreateThread(NULL,0,Thread,0,0,NULL) ;
    return 0 ;
    case WM_COMMAND:
    MessageBox(hwnd,"dfdf","dfdf",MB_OK) ;
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    DefWindowProc(hWnd, message, wParam, lParam);
    return 0 ;
       }
       return 0;
    }//THREAD FUNCTION
    DWORD WINAPI Thread(LPVOID  lp)
    {
    SendMessage(hwnd,WM_COMMAND,THREAD,1L) ;  
    return 0L ;
    };
    请高手看看啊,,测试一下,该线程还是行成了,应该也运行了
    SendMessage(hwnd,WM_COMMAND,THREAD,1L) ; 语句,为何我窗口过程得不到该消息啊......
      

  5.   

    然后就退出线程了.
    使用PostMessage我也试过了,可没有问题的.
      

  6.   

    回复楼上:我是要运得
    MessageBox(hwnd,"dfdf","dfdf",MB_OK) ; ,你有e_mail 吗? 我把整个WORKSPACE 传给你,如何,,
    代码很短,,但运行后没有弹出该
    MessageBox(hwnd,"dfdf","dfdf",MB_OK) ; ....