1. 程式人生 > >WinCE7.0_FindWindow函式線上程中呼叫卡死的現象(2)

WinCE7.0_FindWindow函式線上程中呼叫卡死的現象(2)

/*

 * 對於同一句FindWindow(NULL,L"TestFindWindow");

 * 在按鍵響應中執行耗時約為: 5ms

 * 線上程中執行由於按鍵響應中有 Sleep 存在耗時約為: 10020ms

* Win32 框架下執行的結果與 MFC 相同

 *

 * 按鍵按下響應後的執行 LOG 如下:

starttest FindWindow: CSmartDeviceMFCDlg::OnBnClickedButton2

 end of test FindWindow:CSmartDeviceMFCDlg::OnBnClickedButton2,tick: 5

 start test FindWindow:TestFindWindowThreadProc

 Sleep 0

 Sleep 1

 Sleep 2

 Sleep 3

 Sleep 4

 Sleep 5

 Sleep 6

 Sleep 7

 Sleep 8

 Sleep 9

 end of test FindWindow:TestFindWindowThreadProc(10020)

執行緒'TestFindWindowThreadProc' (0x45e0336) 已退出返回值為 0 (0x0)

*/

DWORDWINAPI TestFindWindowThreadProc(void * pParam) // 查詢Window控制代碼執行緒
{
    DWORD dwTick = GetTickCount();
    printf("\r\nstart test FindWindow:%s\r\n",__FUNCTION__);
    // 線上程中呼叫FindWindow,但實際的執行動作以SendMessage 方式傳送到視窗程序來執行(如果主窗體阻塞???)
    // 從此測試程式碼發現,Button 按鍵響應中的Sleep 阻塞了 FindWindow 的執行; 直接Sleep 結束才執行了 FindWindow 操作
    FindWindow(NULL,L"TestFindWindow");
    printf("\r\nend of test FindWindow:%s(%d)\r\n",__FUNCTION__,GetTickCount() - dwTick);
 
    return 0;
}
 
voidCSmartDeviceMFCDlg::OnBnClickedButton2()
{
    // TODO: 在此新增控制元件通知處理程式程式碼
    DWORD dwTick = GetTickCount();
    printf("\r\nstart test FindWindow:%s\r\n",__FUNCTION__);
    FindWindow(NULL,L"TestFindWindow");
    printf("\r\nend of test FindWindow:%s,tick: %d\r\n",__FUNCTION__,GetTickCount() - dwTick);
    CloseHandle(CreateThread(NULL,0,TestFindWindowThreadProc,NULL,0,0));
 
    for(int i = 0;i < 10;i++)
    {
           Sleep(1000);
           printf("Sleep %d\r\n",i);
    }
}