I have a method public void foo(){} which in turn calls another method public void fooBL(){} which is in the BusinessLogic layer. fooBL() then calls another method fooDAL() which is in the DataAccessLayer. I want to call foo() asynchronously on a button click event. I'm using .NET4.0.
- What did you try already?TheJoeIaut– TheJoeIaut2014-07-14 06:10:30 +00:00Commented Jul 14, 2014 at 6:10
- I read about using task.run and using delegate.BeginInvoke(), but till now i'm not getting a clear idea on which one will be better.Niar– Niar2014-07-14 06:12:19 +00:00Commented Jul 14, 2014 at 6:12
Add a comment |
3 Answers
You can wrap anything in Task.Run to run it via the default scheduler (the ThreadPool):
Task.Run(() => { yourMethodCallHere(); }); You should avoid the use of StartNew if you are unaware of how it works. You could introduce potential bugs as it captures the currently executing TaskScheduler instance and you will no doubt hit "cross-thread" exceptions when dealing with UI elements (or other, much more subtle bugs).
1 Comment
Niar
task.run is not available in .net 4.0