使用线程更新父窗口 [英] Update parent window using thread

查看:101
本文介绍了使用线程更新父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试通过C-Win32编程使用名为MyThread1的线程在窗口上打印随机数.
但是它不会更新主窗口,我看到主窗口总是空白.
下面是伪代码.

Hi,
I am trying to print random numbers on a window using a thread named MyThread1 through C - Win32 programming.
But it does not update the main window and i see the main window being blank always.
Below is the pseudo code.

VOID MyThread1( PVOID pvoid )
{
   HDC hdc;

   TCHAR szBuff[10];
   hdc = GetDC( hwnd );
   wsprintf( szBuff, TEXT("%d"), rand() );
   TextOut( hdc, rand(), rand(), szBuff, lstrlen( szBuff ) );
   ReleaseDC( hwnd );

}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM, wParam, LPARAM lParam )
{
   switch( message )
   {
     case WM_CREATE:
          for( int i = 0; i < 10; i++ )
             _beginthread( MyThread1, 0, NULL );
     case WM_DESTROY:
          PostQuitMessage( 0 );
          break;
   }
   return DefWindowProc( hwnd, message, wParam, lParam );
}



而不是使用多线程,而是使用单线程在主窗口的随机位置打印随机字符.
请让我知道这是正确的方法,还是我在两者之间的某个地方出错.

谢谢



Instead of using multiple threads i m using single thread to print random chars at random position on the main window.
Please let me know is this the right way or am i going wrong somewhere in between.

Thanks

推荐答案

从另一个线程在DC上写入绝对是没有用的,甚至是危险的.所有渲染都应在Windows消息WM_PAINT的处理程序中进行.它总是发生在您的UI线程中.因此,问题是:如何使渲染的图形随时间变化并触发此消息?好吧,不要发送或发布它.通过Windows API Invalidate完成.为了提高性能,您还可以使用参数(矩形或区域)调用InvalidateRectInvalidateRect,以仅使场景的一部分无效.
请参阅 http://msdn.microsoft.com/en-us/library /dd145005%28v=vs.85%29.aspx [ ^ ].

现在,如何从单独的线程更改图形以创建一些动画效果?首先,线程没有意义,因为它只写一次图形数据,但是您可以使线程以一定的延迟在循环中运行,从而在每次迭代中生成不同的文本.为此,要有一个共享内存,例如保存您输出的字符串.由于此内存应由两个线程使用(该字符串将由您创建的线程写入,并由您的UI线程读取),因此应使用关键部分互锁此内存(这是最轻量的方法) ,通过方法EnterCriticalSection/LeaveCriticalSection,请参见:
http://en.wikipedia.org/wiki/Critical_section [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms682608%28v=vs.85%29.aspx [ http://msdn.microsoft.com/zh-CN /library/windows/desktop/ms684169(v=vs.85).aspx [
Writing on DC from the other thread is absolutely useless and even dangerous. All rendering should occur in the handler of a Windows message WM_PAINT. It always happens in your UI thread. So, the question is: how to make the rendered graphics changing with time and trigger this message? Well, not be sending or posting it. It''s done via the Windows API Invalidate. In order to improve performance you can also call InvalidateRect or InvalidateRect with parameter (rectangle or region) to invalidate only the part of the scene.
See http://msdn.microsoft.com/en-us/library/dd145005%28v=vs.85%29.aspx[^].

Now, how to change graphics from a separate thread to create some animation effect? First of all, your thread makes no sense as it writes graphical data only once, but you could make the thread running in cycle with some delay, generating different text in each iteration. To do this, have a shared memory holding, say, the string you output. As this memory should be used by two threads (the string will be written by the thread you created and read by your UI thread), addressing to this memory should be inter-locked using critical section (this is the most light-weight method), via the methods EnterCriticalSection/LeaveCriticalSection, see:
http://en.wikipedia.org/wiki/Critical_section[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684169(v=vs.85).aspx[^].

You additional (non-UI) thread should only modify this data and notify your UI thread about the change by invalidating the client area of the window.

—SA


这篇关于使用线程更新父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆