C#中的GUI线程 [英] GUI Thread in C#

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

问题描述

您好,


我试图创建一个包含大约14个不同线程的应用程序,这些线程必须访问GUI并更新一些标签。现在,如果我这样做,经过一点点时间之后它有点挂起,我认为因为gui已经被两个或多个线程同时加入了。所以我试着用这段代码来解决这个问题:

Hi there,

Iam trying to make an app that has about 14 different threads which have to access the GUI and update some of the labels. Now if i do that, after a little whil it kinda hangs, I think its becaus the gui has been accesed by two or more threads at the same time. So i tried to solve this writing this code:

展开 | 选择 | Wrap | 行号

推荐答案

如果我需要然后通常我会在后台运行一个单独的线程,它会根据它可以访问的变量参数以设定的间隔更新屏幕。显然,如果你同时从多个线程更改它们,这些将需要锁定。


我总是使用委托来防止GUI锁定,我相信类似于你的回调函数,所以我认为你几乎就在那里。


只是要注意,我发现在一个应用程序中lock()停止工作。读完之后,它对于大量重复的连续调用(例如绘图)来说是不可靠的,所以在你使用它时要小心。


Ian
If I need regular updates on screen then normally I have a separate thread running in the background which updates the screen at set intervals based upon variable parameters that it has access to. Obviously these will need locking if you''re changing them from multiple threads at the same time.

I always use delegates when doing this to prevent the GUI locking up, which I believe is similar to your callback functions, so I think you''re almost there.

Just to note, I found that in one application the lock() stopped working. Having read up on it, it isn''t reliable for large numbers of repetitive, successive calls (e.g. drawing), so just be careful when you are using it.

Ian



如果我需要在屏幕上定期更新,那么通常我在后台运行一个单独的线程,它根据可变参数以设定的间隔更新屏幕它有权访问。显然,如果你同时从多个线程更改它们,这些将需要锁定。


我总是使用委托来防止GUI锁定,我相信类似于你的回调函数,所以我认为你几乎就在那里。


只是要注意,我发现在一个应用程序中lock()停止工作。阅读之后,对于大量重复的连续调用(例如绘图)来说,这是不可靠的,所以在使用它时要小心。


Ian
If I need regular updates on screen then normally I have a separate thread running in the background which updates the screen at set intervals based upon variable parameters that it has access to. Obviously these will need locking if you''re changing them from multiple threads at the same time.

I always use delegates when doing this to prevent the GUI locking up, which I believe is similar to your callback functions, so I think you''re almost there.

Just to note, I found that in one application the lock() stopped working. Having read up on it, it isn''t reliable for large numbers of repetitive, successive calls (e.g. drawing), so just be careful when you are using it.

Ian



thx m8 = D.我尝试了另一种方法,将所有数据放在一个数组中,让一个线程用数组中的数据更新lbls。那不起作用=,(。所以我想尝试使用背景工作者,我读的应该更容易使用(只要它适用于我= p)


greetz ,bobido!

thx m8 =D. I tried another way, by putting all the data in an array and have one thread update the lbls with the data in the array. That didnt work =,(. So iam goin to try and use the backgroundworker, which I read should be easier to work with (as long as it works for me =p)

greetz, bobido!


您是使用事件调用这些函数还是只是尝试直接从其他线程调用它?
Are you calling those functions with an event or just trying to call it directly from other threads?


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

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