답변됨 Parallel treatment

  • Tuesday, August 21, 2012 4:19 PM
     
     

    Hi,
    I have a basic "script" that basically read computer names one by one
    from a plane text file and triggers an action for each computer.

    The script takes tremendous time as it goes sequentially.

    I know that recoding it using a more advanced Language such as C# would
    help take advantage of features that will treat the list in a more parralel fashion.

    If I can trigger 10 lines simultanously,
    it will split by a minimum of 10 the total duration of the overall treatment.
    (means that at any given time, 10 computers from that list would be treating the job)

    I guess I'll have to use multithreading - but I'm still on the howTo steps ?

    Thanks.


    MCTS Windows Server Virtualization, Configuration

All Replies

  • Tuesday, August 21, 2012 4:26 PM
    Moderator
     
     Answered

    Yes - if you want to process the items in parallel, you'll likely want to use multithreading.

    I have a blog series on some of the features in .NET 4 that make this far simpler: http://reedcopsey.com/series/parallelism-in-net4/   It may help as a starting point.  In your case, your routine sounds like you can probably use simple data parallelism (http://reedcopsey.com/2010/01/20/parallelism-in-net-part-2-simple-imperative-data-parallelism/) to just loop through the items in parallel and process all of them.  That's likely your best option.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Tuesday, August 21, 2012 4:28 PM
     
     

    Please have a look at the ThreadPool.QueueNewUserWorkItem method.

    http://msdn.microsoft.com/en-us/library/4yd16hza.aspx

    You queue up work to be done in the form of method calls basically. You can pass the argument to the method, and then ThreadPool will execute them in parallel.

    The 'ThreadProc' method used there would be replaced by your 'run some script on computer x'

  • Tuesday, August 21, 2012 4:31 PM
    Moderator
     
     

    Please have a look at the ThreadPool.QueueNewUserWorkItem method.

    http://msdn.microsoft.com/en-us/library/4yd16hza.aspx

    You queue up work to be done in the form of method calls basically. You can pass the argument to the method, and then ThreadPool will execute them in parallel.

    The 'ThreadProc' method used there would be replaced by your 'run some script on computer x'

    FYI - I'd recommend switching away from using TP.QueueUserWorkItem -

    If you're using .NET 4, I'd suggest getting into the habit of using Task.Factory.StartNew, and if you can use .NET 4.5, Task.Run.  These provide many advantages, especially if you want to return values, but also with integration with the new async/await keywords in C# 5.  It's a good idea to consider moving to the newer APIs.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Tuesday, August 21, 2012 5:07 PM
     
     

    Yeah, good point. The old stuff I just know, the new stuff is well, new :p

    There wouldn't happen to be a nice article discussing new features in .NET4 regarding this sort of thing?

  • Tuesday, August 21, 2012 5:31 PM
    Moderator
     
     


    There wouldn't happen to be a nice article discussing new features in .NET4 regarding this sort of thing?

    http://reedcopsey.com/series/parallelism-in-net4/  (I'm biased, given that I wrote it ;) - but I think it's a great intro.)


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Thursday, August 23, 2012 5:13 PM
     
     
    Nice man, thanks!