Wednesday 9 December 2009

C# 3.5 Batch Executor

I wanted to have something that would take a load of tasks I had to run, and run them on multiple threads to make them as efficient as possible. The first time I used this I put in 10 web service calls I needed to make. Running them through this sped them up by 50%.

Another feature of this is that it can be run on the main UI (STA) thread as it uses _manualResetEvent.WaitOne(). If you used WaitHandle.WaitAll you'd have to run on the MTA thread, which may not be convenient. I hope you'll agree it's a nice lightweight simple solution to this problem.

Usage as follows:-

var batchExecutor = new BatchExecutor();
_actions.ForEach(action => batchExecutor.AddMethodToExecute(action.Initialise));
batchExecutor.ExecuteAndWaitUntilAllComplete();


Batch Executor class:-

public class BatchExecutor
{
private readonly List _actions;
private readonly ManualResetEvent _manualResetEvent;
private int _methodsLeftToExecute;

public BatchExecutor()
{
_actions = new List();
_manualResetEvent = new ManualResetEvent(false);
}

public void AddMethodToExecute(Action action)
{
_actions.Add(action);
Interlocked.Increment(ref _methodsLeftToExecute);
}

public void ExecuteAndWaitUntilAllComplete()
{
_actions.ForEach(action => ThreadPool.QueueUserWorkItem(o =>
{
action();

if (Interlocked.Decrement(ref _methodsLeftToExecute) == 0)
_manualResetEvent.Set();
}));

_manualResetEvent.WaitOne();
}
}

21 comments:

  1. First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    http://CSharpTalk.com

    Thanks again
    Rahul

    ReplyDelete
  2. ISO files by having an internal drive that is definitely digital. It could possibly process rather nearly all CD / DVD / BD image files ISO that's together with and data. Power ISO Crack offers you an answer that's all-in-one. You'll be able to positively do every standard thing with your ISO files and disc image files.
    In addition, it really is an archiving software

    ReplyDelete
  3. Thanks for the great message! I really enjoyed reading
    you could be a good writer. Evil Alvzis notes blog and testament
    will finally come back later. I want to support
    keep writing well, have a nice weekend!
    xmedia recode crack
    tweakbit driver updater crack
    vmware workstation pro crack
    windows 10 product key crack

    ReplyDelete
  4. Mycleanpc is an optimization tool and a cleaning software that protects your computer from adware, cache, and other temporary files.
    It also includes a registry cleaner and a privacy protector that will wipe out all unwanted data items that have been collected over time within your browser.
    miracle box crack
    coreldraw graphics suite x3 crack
    avast driver updater crack
    adobe photoshop elements crack

    ReplyDelete
  5. Your website has a great design. Colors and themes that pop!
    Is this a work of your own? Please respond again since I want to construct one of my own.
    and I'd want to know where you've came from on this site
    The item's name is derived from this location.
    Sincerely,
    avg pc tuneup crack
    output arcade crack
    imazing crack activation code
    mocha pro crack

    ReplyDelete
  6. I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues. I do not know how to saqibtech.net But thankfully, saqibtech.net

    poweriso-crack
    driver-talent-pro-crack
    glasswire-elite-crack
    free-youtube-to-mp3-converter-crack
    teracopy-pro-crack

    ReplyDelete
  7. I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues. I do not know how to saqibtech.net But thankfully, saqibtech.net

    poweriso crack
    driver talent pro crack
    glasswire elite crack
    free youtube to mp3 converter crack
    teracopy pro crack

    ReplyDelete
  8. Your site has a great design. Colors and themes that appear!
    Is this your job? Please answer again because I want to build it myself.
    and I want to know where you are on this page
    The item's name comes from this location.
    Honestly,
    pycharm crack
    norton internet security crack
    avocode crack
    ashampoo music studio crack

    ReplyDelete
  9. Thanks for sharing such a great article with us. This surely helps me in my work. Thanks a lot.
    Death Stranding Crack
    FIFA 22 Crack
    unhackme pro crack
    Valheim Crack

    ReplyDelete

  10. Oh my goodness! Impressive article dude! Thanks, However I am having troubles with your RSS. I don’t understand the reason why I am unable to join it. Is there anybody having the same RSS issues? Anyone that knows the answer will you kindly respond? Thanx!!
    dead age hi2u crack
    voice recorder apk
    protonvpn crack
    google play store android crack
    sid meiers civilization new frontier pass part crack
    avast premium security crack
    mindgenius business crack

    ReplyDelete
  11. You have a great site, but I wanted to know if you know.
    Any community forum dedicated to these topics.
    What was discussed in this article? I really want to be a part of it.
    A society in which I can obtain information from others with knowledge and interest.
    Let us know if you have any suggestions. I appreciate this!

    omnisphere crack
    omnisphere crack
    omnisphere crack
    omnisphere crack
    omnisphere crack
    omnisphere crack
    omnisphere crack

    ReplyDelete
  12. Thank you for the information.
    it has been quite beneficial to me.
    I'm hoping for a few more postings from you.
    artisteer crack
    macromedia flash Crack
    malwarebytes anti malware crack
    kaspersky internet security crack

    ReplyDelete
  13. I saw your writing skills. Your writing skills are amazing. I also really like your ability to write.
    Your writing skills have given me a lot of perspective on this subject. I think you're an old blogger.
    connectify hotspot crack
    tsr watermark image pro crack
    zookaware pro crack
    teamviewer meeting crack
    windows 10 education crack
    gridinsoft trojan killer crack
    aiseesoft screen recorder crack
    driver genius pro crack

    ReplyDelete
  14. Hi! Please know how much I appreciate your site and how much I look forward to the new content you provide.
    For which of your blog posts do I have to pay?
    Interested parties are encouraged to share their knowledge of other online services that may be of interest to me.
    It's really you.
    sam broadcaster pro crack
    cyberlink photodirector ultra crack
    express vpn crack keygen
    vcarve pro crack
    ntlite crack

    ReplyDelete
  15. I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me.
    Thanks for sharing this post is an excellent article. Keep it up. I use the same blogging platform that you have and have.
    it Is Very Informative Thanks For Sharing. I have also Paid This sharing. I am ImPressed For With your Post Because This post is very.
    edius 8
    telly erp crack
    etimetracklite
    poweriso crack
    movavi video converter crack

    ReplyDelete
  16. A big thanks to you for sharing this with everyone you know who may be interested in this subject matter.
    My website is also available for your reference.
    We might be able to work together on a link trade deal.
    adobe premiere elements crack
    nordvpn crack
    avg pc tuneup crack
    wondershare dr fone crack

    ReplyDelete

  17. You are so interesting! I don't think I've read anything like this before.
    It's great to find someone with real ideas on this topic. Indeed ... thank you very much for starting.
    This site is something needed on the internet, not real!
    This is a great blog! Your site is loading too fast!
    What type of web server do you use? Can you send me an affiliate link for your web host?

    apeaksoft mobietrans crack
    high logic fontcreator crack
    coolmuster pdf creator pro crack
    acdsee photo studio home crack
    teamviewer crack
    abelssoft bankingbrowser crack
    kms 2038 digital online crack
    amolto call recorder premium for skype crack
    windows password recovery tool ultimate crack
    adguard premium crack

    ReplyDelete
  18. Amazingly pertinent and helpful data. Wanted to encourage you to keep up the excellent work!
    device doctor full crack
    wise care 365 pro crack
    mixcraft crack
    sketch 92 crack

    ReplyDelete

  19. it is the best website for all of us. it provides all types of software which we need. you can visit this website.
    majorjeekm
    liveprocrack
    livecrackerm
    sunnypc

    ReplyDelete
  20. Avast Secure Browser 110.0.20188.100 crack remains protected, isolated, and then convenient net browser which stops hackers after larceny their figures. Consuming it, user can simply mask all user kind connected to protect their isolated data benign; hunks spiteful websites then transfers, and practice encryption toward protected their statistics.
    avast secure browser crack

    ReplyDelete