C#:填充使用单独的线程的UI [英] C#: Populating a UI using separate threads

查看:139
本文介绍了C#:填充使用单独的线程的UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做出一些感觉了香港专业教育学院被移交为了追踪到错误的来源的应用程序。那里有一点点code(这里是简化的)它创建四个线程它在主窗体上依次填充列表视图。每种方法获取数据从数据库中检索,并从资源DLL图形,以便直接填充一个ImageList和ListView。

I'm trying to make some sense out of an application Ive been handed in order to track down the source of an error. Theres a bit of code (simplified here) which creates four threads which in turn populate list views on the main form. Each method gets data from the database and retrieves graphics from a resource dll in order to directly populate an imagelist and listview.

。据我阅读这里(<一href="http://stackoverflow.com/questions/2302982/a-problem-in-c-using-threads-and-listview">link)从非UI线程以外的任何线程更新UI元素不应该做的,然而这似乎工作?

From what Ive read on here (link) updating UI elements from any thread other than the UI thread should not be done, and yet this appears to work?

Thread t0 = new Thread(new ThreadStart(PopulateListView1));
t0.IsBackground = true;
t0.Start();

Thread t1 = new Thread(new ThreadStart(PopulateListView2));
t1.Start();

Thread t2 = new Thread(new ThreadStart(PopulateListView3));
t2.Start();

Thread t3 = new Thread(new ThreadStart(PopulateListView4));
t3.Start();

的误差本身是一个System.InvalidOperationException图像不能被添加到ImageList。其中有我想知道,如果上述code被以某种方式联系在一起的。

The error itself is a System.InvalidOperationException "Image cannot be added to the ImageList." which has me wondering if the above code is linked in some way.

IIS填充UI建议,如果没有什么是可能出现的并发症,从它产生的?这个方法

Iis this method of populating the UI recommended and if not what are the possible complications resulting from it?

更新:

我可能已经通过参照形式给出了一些误传。该应用程序是一个Windows窗体应用程序,但code是基于一个用户控件一个插件应用程序。线程是一个INITIALISE方法,通过这个控制公开暴露的内部创建的。该列表视图等也是这个插件用户控件的一部分。

I may have given some misinformation by referring to a 'form'. The application is a windows forms application but the code is from a plugin application based on a usercontrol. The threads are created inside an initialise method publically exposed by this control. The listviews etc are also part of this plugin usercontrol.

推荐答案

在你的线程的方法,如<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork%28v=VS.95%29.aspx"相对=nofollow>的DoWork()的<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=VS.95%29.aspx"相对=nofollow> BackgroundWorker的类的,例如,您将需要instiate委托方法来填充你的UI控件。然后,验证你的UI控件是否需要被调用(的 InvokeRequired 属性),然后调用它的时候it'srequired来。

Within your threads methods, such as DoWork() for the BackgroundWorker class, for example, you will need to instiate a delegate method to populate your UI control. Then, verifying whether your UI control requires to be invoked (InvokeRequired property), then invoking it when it'srequired to.

private delegate IList<MyObject> PopulateUiControl();

private void myThread_DoWork(object sender, DoWorkEventArgs e) {
    PopulateUiControl myDelegate = FillUiControl;

    while(uiControl.InvokeRequired)
        uiControl.Invoke(myDelegate);
}

private IList<MyObject> FillUiControl() {
    uiControl.Items = myThreadResultsITems;
}

这不是一个precise工作code,因为我不能花时间做研究,等等,都必把你的路径取得成功。

It is not a precise working code, as I can't take the time to do the research, etc. but this shall put you in the path to succeed.

在最后,我同意其他人,尽量避免这样的事情在未来,因为它可以得到棘手的调试或透露一些奇怪的行为。也许.NET 4对主题有所改善微软一直努力使并行易于使用多核处理器进行开发。

In the end, I agree with the others, try to avoid such things in the future, as it can get tricky to debug or reveal some strange behaviour. Perhaps .NET 4 has some improvements on the topic as Microsoft has worked hard to make parallelism easy for the use of multicore processors for developers.

这篇关于C#:填充使用单独的线程的UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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