Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 25-Oct-10 11:35am
v2
Comments
HimanshuJoshi 25-Oct-10 17:35pm    
Edited to remove extra punctuation marks.

1 solution

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)...
C#
//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();
}
}


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

add this to your base class :
C#
class baseclass{
Dictionary<int, file> yourDictionary;
}


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 wrote:
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:
C#
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 wrote:
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?


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:
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900