我的理解是就只有一個執行緒,也就是主執行緒。但是內部的代碼非常多,沒看通不確定,所以過來問下。
下面是我的理解:創建全域物件theApp,然后在afxwmain中呼叫該物件的InitInstance方法,在這個方法中創建了一個對話框(這里用對話框程式來舉例)。
對話框運行domodal,就停在那里(阻塞)。 從這個順序來看,是主執行緒一路執行這些動作。但是為啥run還能運行訊息回圈?
這個問題很困惑。基礎差,不能理解。
第二個大問題,CWinApp繼承自CWinThread,而CWinThread應該屬于執行緒類,就是專門和執行緒有關系的一個類。我不解的是,從上面分析來看,就一個主執行緒在運行,似乎也沒有開辟新的執行緒(如果沒錯的前提下),為什么CWinApp非要繼承CWinThread呢?這個繼承有什么意義嗎?
給分50,如果認真答了,分全給,謝謝。
uj5u.com熱心網友回復:
dlg程式 就一個執行緒啊。
uj5u.com熱心網友回復:
#include <windows.h>
// 回呼函式:接收和處理訊息
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
MSG msg;
HWND hWnd;
static TCHAR szAppName[] = TEXT("HelloWindow");
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))// 注冊表單類
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
// Creates an overlapped, pop-up, or child window
hWnd = CreateWindow(
szAppName, // A null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function
TEXT("The Hello Window Program"), // The window name
WS_OVERLAPPEDWINDOW, // The style of the window being created
CW_USEDEFAULT,// The initial horizontal position of the window
CW_USEDEFAULT,// The initial vertical position of the window
CW_USEDEFAULT,// The width, in device units, of the window
CW_USEDEFAULT,// The height, in device units, of the window
NULL, // A handle to the parent or owner window of the window being created
NULL, // A handle to a menu, or specifies a child-window identifier depending on the window style
hInstance, // program instance handle
NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
這段代碼沒用MFC也可以創建一個視窗,代碼比較簡單,可以簡單了解一下,特別注意以下幾處:
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
wndclass.lpfnWndProc = WndProc;// 設定回呼函式,接收處理訊息
while (GetMessage(&msg, NULL, 0, 0))
{
while (GetMessage(&msg, NULL, 0, 0))
DispatchMessage(&msg);
}
MFC對WINAPI進行了簡單封裝
向導根據選擇生成了通用代碼
對熟悉MFC, 特別是其中“訊息映射”、“事件驅動”使用起來比較方便,并且可視化 開發效率高,但這個自學起來確實有點難。
可以試著簡單了解一下這段代碼,再看向導生成的MFC代碼,可能會容易一下
不知道說的對不對,僅供參考
uj5u.com熱心網友回復:
你回復的太基礎了,也沒看清我問的問題,不過很熱心謝謝了。
uj5u.com熱心網友回復:
CWinThread封裝的不好,說句實在話,MFC整體封裝都不夠優雅。所以你產生的這種疑問是正常的。
win32編程還是得稍微熟悉一下,行程創建的時候就有個默認執行緒了,就是跑main函式就算一個執行緒了。
弄個對話框程式,把MFC弄成靜態鏈接,可以單步進去看代碼。
uj5u.com熱心網友回復:



uj5u.com熱心網友回復:
建議樓主熟悉一下MFC的除錯,還有看問題要看到本質domal訊息阻塞,它到底是怎樣阻塞的,你要弄懂,不能一知半解。
uj5u.com熱心網友回復:
看Win32應用程式,熟悉下訊息機制uj5u.com熱心網友回復:
win32的程式,一般都會是:主執行緒+n個輔助執行緒uj5u.com熱心網友回復:
任務管理器的行程標簽,其中一列可以顯示執行緒數啊...如果看不到點上面的查看->選擇列->打勾執行緒數uj5u.com熱心網友回復:
只能找侯杰的 深入淺出MFC
uj5u.com熱心網友回復:
你暫停程式除錯視窗不是能看到幾個執行緒嗎?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/35232.html
標籤:進程/線程/DLL
