C++ 运行 一个异步函数而不阻塞 ui
C++ run a function in async and not block the ui
我有一个检查 dotnet 框架的小应用程序,如果没有安装它会安装它
现在,当应用程序启动时,我想弹出一个 gif 图像,其中包含加载之类的内容,并在后台检查框架并安装。
这里的问题是它不能有任何先决条件ui站点到运行应用程序
这是我目前所拥有的
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
int exitCode = -1;
showPic(hInstance);
MessageBox(0L, L"Dotnet will installed", L"Alert", 0);
auto fut = std::async(std::launch::async, DoWork, hInstance, lpCmdLine);
fut.wait();
exitCode = fut.get();
return exitCode;
}
showPic()
void showPic(HINSTANCE hInstance)
{
loadImage(hInstance);
// create window
wnd = createWindow(hInstance);
SetWindowLong(wnd, GWL_STYLE, 0);
ShowWindow(wnd, SW_SHOW);
}
loadImage(HINSTANCE hInstance)
void loadImage(HINSTANCE hInstance)
{
imageDC = CreateCompatibleDC(NULL);
imageBmp = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
imageBmpOld = (HBITMAP)SelectObject(imageDC, imageBmp);
}
现在这里发生的事情是,如果我不显示消息框,图片不会加载到 window,而且 window 仍然进入无响应模式,我也无法得到它适用于 gif,仅适用于 bmp 图像
任何帮助都适用
现在因为我等待 fut 很明显它会阻塞 ui 直到它有值,解决方法是什么
这应该很简单。创建 window,显示它,调用线程,转到主消息循环。线程完成后,它将销毁 window.
struct T_data {
HWND hWnd;
HINSTANCE hInstance;
LPTSTR cmdline;
int exitCode;
};
DWORD WINAPI taking_too_long(LPVOID p) {
Sleep(2000); //wait at least 2 seconds!
T_data* data = reinterpret_cast<T_data*>(p);
auto fut = std::async(std::launch::async, DoWork, data->hInstance, data->lpCmdLine);
fut.wait();
data->exitCode = fut.get();
//make sure the window handles IDM_EXIT to close itself
PostMessage(data->hWnd, WM_COMMAND, IDM_EXIT, 0);
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int) {
T_data data;
data.exitCode = -1;
data.hWnd = hWnd;
data.hInstance = hInstance;
data.cmdline = lpCmdLine;
data.hWnd = showPic(hInstance);
CreateThread(NULL, 0, taking_too_long, &data, 0, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return data.exitCode;
}
我有一个检查 dotnet 框架的小应用程序,如果没有安装它会安装它
现在,当应用程序启动时,我想弹出一个 gif 图像,其中包含加载之类的内容,并在后台检查框架并安装。
这里的问题是它不能有任何先决条件ui站点到运行应用程序
这是我目前所拥有的
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
int exitCode = -1;
showPic(hInstance);
MessageBox(0L, L"Dotnet will installed", L"Alert", 0);
auto fut = std::async(std::launch::async, DoWork, hInstance, lpCmdLine);
fut.wait();
exitCode = fut.get();
return exitCode;
}
showPic()
void showPic(HINSTANCE hInstance)
{
loadImage(hInstance);
// create window
wnd = createWindow(hInstance);
SetWindowLong(wnd, GWL_STYLE, 0);
ShowWindow(wnd, SW_SHOW);
}
loadImage(HINSTANCE hInstance)
void loadImage(HINSTANCE hInstance)
{
imageDC = CreateCompatibleDC(NULL);
imageBmp = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
imageBmpOld = (HBITMAP)SelectObject(imageDC, imageBmp);
}
现在这里发生的事情是,如果我不显示消息框,图片不会加载到 window,而且 window 仍然进入无响应模式,我也无法得到它适用于 gif,仅适用于 bmp 图像 任何帮助都适用
现在因为我等待 fut 很明显它会阻塞 ui 直到它有值,解决方法是什么
这应该很简单。创建 window,显示它,调用线程,转到主消息循环。线程完成后,它将销毁 window.
struct T_data {
HWND hWnd;
HINSTANCE hInstance;
LPTSTR cmdline;
int exitCode;
};
DWORD WINAPI taking_too_long(LPVOID p) {
Sleep(2000); //wait at least 2 seconds!
T_data* data = reinterpret_cast<T_data*>(p);
auto fut = std::async(std::launch::async, DoWork, data->hInstance, data->lpCmdLine);
fut.wait();
data->exitCode = fut.get();
//make sure the window handles IDM_EXIT to close itself
PostMessage(data->hWnd, WM_COMMAND, IDM_EXIT, 0);
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int) {
T_data data;
data.exitCode = -1;
data.hWnd = hWnd;
data.hInstance = hInstance;
data.cmdline = lpCmdLine;
data.hWnd = showPic(hInstance);
CreateThread(NULL, 0, taking_too_long, &data, 0, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return data.exitCode;
}