1

i have a need to invoke some methods asynchrounously. On startup, the app does some work on initializing a few things but I don't want the UI to be held up. So, this function is performed in a separate thread using Threadpool queueitem method.

I was thinking to have a few methods similar to below.

protected void Page_Load(object sender, EventArgs e)
{
    DoAsyncOperation(new Func<string, int>(TestMethod), "test");
}

private int TestMethod(string p)
{
    return 0;
}

public bool DoAsyncOperation(Delegate asyncMethod, params object[] args)
{
    return ThreadPool.QueueUserWorkItem( delegate {asyncMethod.DynamicInvoke(args);});
} 

so that in future other methods can also be run async if needed and its all in one place. If I have to change logic for the threading, I can do it in a single method.

Is there any performance issues with this approach, passing in the method dynamically?

sehe
  • 374,641
  • 47
  • 450
  • 633
Alex J
  • 1,547
  • 2
  • 26
  • 41

1 Answers1

4

As always: "Profile Profile Profile"! That said, in this case I'd be fairly sure to say:

No not at all at this scale, it doesn't matter:

The actual time spent loading the page or even managing the Queue far outweighs the invocation overhead of a delegate (even if it uses DynamicInvoke under the hood)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you for your response. Can you suggest some ways I can profile this, just to learn. – Alex J Nov 30 '11 at 12:17
  • @AlexJ: I'd suggest [MiniProfiler for ASP.Net (and ASP.Net MVC)](http://code.google.com/p/mvc-mini-profiler/) or pick any one of the general purpose [.NET Profiler tools from here](http://stackoverflow.com/questions/3927/what-are-some-good-net-profilers) – sehe Nov 30 '11 at 12:39