如何使用 vc++ 在子线程中创建和更新窗口
我刚刚编写了一个子线程来创建和更新窗口,但我遇到了一些问题。线程执行完成后(自然地)我的窗口会自动关闭。但我不想关闭它,所以我尝试在线程中放置一个 while 循环,并在该循环中调用 InvalidateRect()
函数,以便它可以更新 window.现在窗口不会自动关闭,但我无法移动它或与其交互,并且光标还显示一些忙碌的图标(意味着完全没有响应)。我怎样才能解决这个问题。下面是代码:
从 main() 调用此函数
bool CameraApp::OnInit()
{
hThread = (HANDLE)_beginthreadex( NULL, 0, &CameraFrame::StartCameraPreview,
NULL, 0, &threadID );
WaitForSingleObject( hThread, INFINITE );
CloseHandle( hThread );
return TRUE;
}
线程功能块
unsigned __stdcall CameraFrame::StartCameraPreview( void* pArgs )
{
cFrame.ShowCameraWindow();
while(1)
{
cFrame.StartCapture();
InvalidateRect(hwnd, NULL, false);
Sleep(5000);
}
_endthreadex( 0 );
return 0;
}
我无法使用 main()
函数来创建窗口。因此,我必须使用线程并使用从网络摄像头拍摄的周期性图像更新该窗口。
I just coded a child thread to create and update window but I am facing some problem. My window closes automatically after that thread execution is completed (naturally). But I don't want to close it so I tried putting a while loop in thread and in that loop I am calling InvalidateRect()
function so that it can update window. Now window is not closing automatically but i can't move it or interact with it and cursor also showing some busy icon(means completely not responding). How I can solve that problem. below is code:
calling this from main()
bool CameraApp::OnInit()
{
hThread = (HANDLE)_beginthreadex( NULL, 0, &CameraFrame::StartCameraPreview,
NULL, 0, &threadID );
WaitForSingleObject( hThread, INFINITE );
CloseHandle( hThread );
return TRUE;
}
Thread function block
unsigned __stdcall CameraFrame::StartCameraPreview( void* pArgs )
{
cFrame.ShowCameraWindow();
while(1)
{
cFrame.StartCapture();
InvalidateRect(hwnd, NULL, false);
Sleep(5000);
}
_endthreadex( 0 );
return 0;
}
i can't use main()
function to create window. So, i have to use thread and update that window with periodic image taken from web-camera.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在处理 Windows 消息的辅助线程中创建消息泵,而不是无限循环。
Instead of your infinite loop you need to create message pump in secondary thread that processes windows messages.