帮助线程管理! [英] Help with threads management!

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

问题描述

嗨!

我正在创建一个下载管理器程序,该程序可以下载多个线程中的文件(每个线程一个文件).但是我在更新UI控件时遇到了一些问题(最难的
工作已覆盖ListView组件).我必须在创建它们的线程上更新控件.当创建DownloadUnit对象(封装单个下载的类)时,我通过ControlsManager<<<< SINGLETON>>类(仅存在此类的一个实例).是
就像 MEDIATOR (模式中介")一样,可集中管理不同应用程序层.

因此,我创建的每个DownloadUnit对象,然后从ControlsManager方法添加事件处理程序.因此,所有DownloadUnit对象都具有相同的事件处理程序.这是我的UI更新缓慢的原因,因为每个线程都适用于一个方法吗?

我计划在
中创建DownloadUnitHandler的一些列表 每个DownloadUnit
ControlsManager 这样每个下载线程都可以拥有自己的事件处理程序副本.
矫正吗?您能给些建议吗?

谢谢

Hi!

I''m creating a download manager program which can download files in many threads (one file per thread). But I experience some problems with updating UI controls ( the hardest
work is overriden ListView component). I have to update controls on a thread that created them. When I create a DownloadUnit object (my class that encapsulates single download) then I attach event handlers to it through my ControlsManager <<SINGLETON>> class (only one instance of this class exists). It''s
like a MEDIATOR ( pattern "Mediator") that centralizes management of different layers of application.

So each DownloadUnit object I create, I then add an event handler from ControlsManager method. So all DownloadUnit objects have the same event handlers. Is it a reason of my slow UI updating because each thread applies to one method?

I plan to create some list of DownloadUnitHandler inside
a ControlsManager for each DownloadUnit
so each downloading thread can have it''s own copy of event handlers.
Is it corrective? Can you please advice something beter?

Thanks

推荐答案

在我看来,问题是您的调解人在错误的地方.....理想情况下...您想做的是被击中...在应用程序启动时从主线程中分离出来,并从该线程中生成"forms"线程,发送对您的Singelton的引用,(您可能需要为此编写特殊的结构才能通过在一些其他信息中..最好提前进行此操作,因为您将添加到此结构中)...
the problem it looks like to me is that your mediator is in the wrong place..... Well Ideally ... what you would want to to is hit this Head on... split off of your main thread on application start and spawn the "forms" thread from this one, sending in a reference to your Singelton, (you will need to probably make a special struct for this to pass in some additional info.. best to do this ahead of time, because you''ll be adding to this struct)...
//allong the lines of 

program//|| your Form(make it hidden):: I'm calling this your "base class"
{

main()//see above (use new form)
{
     thread UI;
     singeltonWrapper package = new SingeltonWrapper(new singelton());
     //this is a Reference type
     Hidden((object)package); // this is now your mediator thread..
     this.spawn_worker threads();
}
}



完成此操作后,将您的(通用?)收藏集说成Dictionary<int,>

将此添加到您的基类中:



once youve done this, put your (generic?) collection lets say Dictionary<int,>

add this to your base class :

class baseclass{
Dictionary<int, file> yourDictionary;
}



并将其重新添加到创建者的包装中...

现在,您可以将所有信息与您的调解人往来...


添加一些访问器和获取器,并使它们与窗体上的事件对话,从您的基类线程中产生您的线程....而不是您的UI线程..

还记得记住将sleep(1)放在适当的位置以允许您的UI线程刷新...或者间隔一定的状态.join(方法,不在声明的名称空间中)以拉回状态数据....

至少那是我过去所做的方式...

根据您的问题:



and add this back into your wrapper in the creator...

now you pump all info to and from your mediator...


add a few accessors and getters, and have them speak with events on the form, spawning your threads from your base class thread.... NOT your UI thread..

also remember to put sleep(1)''s in where appropriate to allow your UI thread to refresh... alternatively at intervals to a status.join(approach, not in a declared namespace) to pull back state data....

at least that is how I''ve done it in the past...

AS per your questions:

Nick Reshetinsky写道:
Nick Reshetinsky wrote:

因此,我创建了每个DownloadUnit对象,然后从ControlsManager方法中添加了一个事件处理程序.因此,所有DownloadUnit对象都具有相同的事件处理程序.这是我的UI更新缓慢的原因,因为每个线程都适用于一个方法吗?

So each DownloadUnit object I create, I then add an event handler from ControlsManager method. So all DownloadUnit objects have the same event handlers. Is it a reason of my slow UI updating because each thread applies to one method?



不应该这样,因为该方法已复制到每个线程中...如果您仍然担心,可以通过以下方法进行测试:



shouldn''t be, because the method is copied out to each thread... if however you are still concerned you can test this by:

your_event_Handeler(blah)
{
int hold = thread.ID;
// bulk of your method here
if(hold!=thread.ID){throw new exception ("threads are all using the same block of code");
}


可能需要花点时间来抛出..但是您可以在调试时仔细检查..

如果它确实在整个方法中都使用了关键区域,那应该可以解决问题


it may take a wile for this to throw.. but you can do this while debugging to double check..

if it does use critical region over the bulk of the method, that should fix the problem

Nick Reshetinsky写道:
Nick Reshetinsky wrote:

我计划在
中创建一些DownloadUnitHandler列表. 每个DownloadUnit都有一个ControlsManager,因此每个下载线程都可以拥有自己的事件处理程序副本.
矫正吗?您能给些建议吗?

I plan to create some list of DownloadUnitHandler inside
a ControlsManager for each DownloadUnit so each downloading thread can have it''s own copy of event handlers.
Is it corrective? Can you please advice something beter?



不需要...只是仔细地管理您的内存(互斥体),它应该处理所有事情...

希望这能使您走上正确的道路. :cool:



shouldn''t need to... just manage your memory (mutex) carefully and that should take care of everything...

Hope this gets you on the right path.. Cheers. :cool:


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

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