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();
}
}

40 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. good~ keep sharing with us, please....I will waiting your up date everyday!! Have a nice day........................................

    ReplyDelete
  3. If you lend someone $20 and never see that person again, it was probably worth it.............................................

    ReplyDelete
  4. 在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................

    ReplyDelete
  5. 逛到你的部落格,第一個感覺就很好,希望每次更新都是美好的開始,也祝你天天都都開心喔........................................

    ReplyDelete
  6. 不要把生命看得太嚴肅,反正我們不會活著離開。......................................................

    ReplyDelete
  7. 向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

    ReplyDelete
  8. 什麼樣的學習計畫並不重要,重要的是你是什麼樣的人。............................................................

    ReplyDelete
  9. 偶爾上來逛逛,下次不知是否還有緣再進來,先祝您平安順利!!!............................................................

    ReplyDelete
  10. 愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................................

    ReplyDelete
  11. 我愛那些使自己的德行成為自己的目標或命定的人................................................

    ReplyDelete
  12. 噴泉的高度,不會超過它的源頭。一個人的事業也是如此,它的成就絕不會超過自己的信念。........................................ ........................

    ReplyDelete