0

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.

2
  • What did you try already? Commented 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. Commented Jul 14, 2014 at 6:12

3 Answers 3

2

It depends on what your goal is.

The simplest way to call foo asynchronous is:

Task t = new Task.Factory.StartNew(foo); 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use thread to run method asynchronously. Below is an example:

Thread t = new Thread(new ThreadStart(foo)); t.Start(); 

I hope it helps you. :)

Comments

1

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

task.run is not available in .net 4.0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.