Windows应用程序中的线程 [英] Threading in windows application

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

问题描述



我有一个主窗体,它包含一个停靠面板,正在其中加载子窗体.
我正在使用反射来获取子窗体实例,这需要花费时间并冻结应用程序.我曾尝试使用后台工作程序和普通线程创建实例的线程.花费时间是由于从Web服务获取数据以及绑定控件.

我正在例外.在一个线程上创建的控件无法在不同线程上的控件上访问.

谁能帮帮我.

在此先感谢

Hi,

I have a Main Form it contains a dock panel in which am loading the child forms.
I am getting the child form instance using reflection, where it takes time and freezes the Application. I have tried threading where i have created the instance using background worker and also normal threading. Time taken is due to fetching data from web services as well as binding the controls.

I am getting exception. Control created on one thread cannot be accessed on a control in different thread.

Can any one help me out.

Thanks in advance

推荐答案

您必须在其他线程中使用invoke来控制ui元素.看看链接

http://www.yoda.arachsys.com/csharp/threads/winforms.shtm [ ^ ]

祝你好运!
You have to use invoke from within other threads to control the ui elements. Have a look at the link

http://www.yoda.arachsys.com/csharp/threads/winforms.shtm[^]

Good luck!


您得到的是交叉线程异常.

当您在两个不同的线程中有一个wo UI时,您不能简单地在它们之间交换数据.您需要使用委托来插入数据.

例如,如果我在线程1中有一个Form A,并且在另一个要在Form A中填充的线程中需要数据,则需要首先创建一个指向线程2的委托,然后使用this.invoke访问该数据.

让我知道您是否需要代码段.
What you are getting is a cross threading exception.

When you have a wo UI in two different threads, you cannot simply interchange data between them. You need to use delegate to interchage data.

For example If I have a Form A in Thread 1 and there I need data in another thread that I want to populate in Form A, I need to first create a delegate pointing to thread 2 and then use this.invoke to access that data.

Let me know if you need a code snippet.


您好,

通常,不能通过线程访问表单的控件.要访问表单控件,请使用委托并尝试执行您的过程.这可能会解决您的问题.

问候,
Suresh
Hi,

Generally, one cannot access a form''s control through a Thread. To access the form controls, use Delegates and try to do your process. This might solve your problem.

Regards,
Suresh


这篇关于Windows应用程序中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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