如何更新不同线程中的组件? .NET CF [英] How to update components in different threads? .NET CF

查看:162
本文介绍了如何更新不同线程中的组件? .NET CF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个ad hoc组件,用来绘制它自己的图片。

我编写了一个.NET Compact Framework应用程序,它在PDA上显示地图。在整个地图中,使用这些组件中的几个组成大图。我这样做是为了避免在一个步骤中绘制整个地图的延迟。

我想知道的是在自己的线程中绘制这些片段,所以地图看起来像一个单一的实体一样成长,并且(更重要的是)避免冻结其余的用户界面。



知道每一个地图绘制在其onPaint方法中。我的想法是,以某种方式告诉系统请在线程中执行此代码。



类似于:

  protected override void OnPaint(PaintEventArgs e)
{
//<要在线程中执行的代码>
e.Graphics.paintTHis();
e.Graphics.paintThat();
whateverItTakesToPaintThisPieceOfTheMap();
//< /要在线程中执行的代码>
}

你知道该怎么做吗?或者我的方法根本不对?



感谢您的时间!

解决方案

<这种做法是错误的。更新ui的代码必须在ui线程上运行。如果您从另一个线程更新ui,您将得到一个异常。


I´m programming a .NET Compact Framework application which shows maps on a PDA.

I´ve created an ad hoc component that paints it´s own piece of the whole map, using several of this components the big picture is composed. I did it this way to avoid the latency of painting the whole map in a single step.

What I would like to do know is to paint this pieces in their own thread, so the map appears to grow as a single entity and (also, and more important) to avoid freezing the rest of the user interface.

Right know each piece of the map is painted in its onPaint method. My idea is to, somehow, tell the system "execute this code in a thread please".

Something like:

protected override void OnPaint(PaintEventArgs e)
    {
       // <code to be executed in a thread>
       e.Graphics.paintTHis();
       e.Graphics.paintThat();
       whateverItTakesToPaintThisPieceOfTheMap();
       // </code to be executed in a thread>
    }

Do you know how to do this? Or is my approach simply wrong?

Thanks for your time!

解决方案

The approach is wrong. Code that updates the ui has to run on the ui thread. You'll get an exception if you update the ui from another thread.

这篇关于如何更新不同线程中的组件? .NET CF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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