views:

538

answers:

7

Hello,

I need to execute 3 parallel tasks and after completion of each task they should call the same function which prints out the results.

I don't understand in .net why we have Asychronous calling (delegate.BeginInvoke() & delegate.EndInvoke()) as well as Thread class?

I'm little confused which one to use when? Now in this particular case, what should I use Asychronous calling or Thread class?

I'm using C#.

A: 

If you have a long running process (e.g. a form application, IIS, Web Service, Windows Service) you will likely be better off using the Async methods. Threads require system resources and overhead. I always try to avoid creating threads. If I can't, I try to rely on ThreadPool to handle/manage them.

You might get more applicable info if you can tell us what your application is.

BeginInvoke uses a delegate behind the scenes where as the Async methods usually don't.

No Refunds No Returns
A: 

Delegate.BeginInvoke grabs a threadpool thread and executes the passed delegate on that thread. So when you use BeginInvoke the .NET Framework does a lot of work like creating new thread, starting it etc.

You can also take a look at ThreadPool.QueueUserWorkItem(delegate { /* do stuff */ }); for an alternative approach to async calls.

Aseem Gautam
+2  A: 

A async method essentially abstracts away the way the work is actually being processed. It may be spawned out into a new process, it may be executed in a separate thread...It doesn't matter.

All that matters is you are saying:

  1. Run this code when you start.
  2. And run this code when you finish.

If given the choice, I'll use a API async method over implementing my own threading mechanism every-time. The framework developers did the hard work for you, why reinvent the wheel.

FlySwat
A: 

Asynchronous delegates are executed using a thread from the thread pool. This reduces the overhead of manually creating a thread and disposing it. Threadpool threads have lesser overhead than the ones that you create manually and has to be disposed.

Also, executing a method in a manually created thread gives you more control such as the ability to interrupt the thread, abort it, check its state, set its priority etc.

Async delegates are used if you want to quickly make a method execute asynchronously.

Also, EndInvoke allows you the return an object out which allows you to retrieve the results of the execution. A Thread.Join, although functionally equivalent, does not allow you to return anything.

Bobby Alexander
+4  A: 

I don't understand in .net why we have Asychronous calling (delegate.BeginInvoke() & delegate.EndInvoke()) as well as Thread class?

  • Asychronous calling is used when you have work items that should be handled in the background and you care when they finish.

  • Thread pools are for when you have work items that should be handled in the background and you don't care when they finish.

  • Threads are for doing stuff that never finishes.

Examples:

If you are reading a large file from disk and don't want to block the GUI thread, use an async call.

If you are lazily writing one or more files in the background, use a thread pool.

If you are polling the file system every few seconds looking for stuff that changed, use a thread.

Jonathan Allen
This is actually pretty wrong.
FlySwat
To elaborate, there are built in classes for most of examples in .NET that handle these without needing to use a threadpool manually.
FlySwat
I don't see how calling QueueUserWorkItem is any more "manual" than calling BeginInvoke.
Jonathan Allen
+7  A: 

1. Asynchronous Delegates

Asychronous calling is used when you have work items that should be handled in the background and you care when they finish.

http://stackoverflow.com/questions/1506838/backgroundworker-vs-background-thread/1507337#1507337

2. BackgroundWorker

  • Use BackgroundWorker if you have a single task that runs in the background and needs to interact with the UI. and use it if you don't care when they finish their task. The task of marshalling data and method calls to the UI thread are handled automatically through its event-based model.
  • Avoid BackgroundWorker if (1) your assembly does not already reference the System.Windows.Form assembly, (2) you need the thread to be a foreground thread, or (3) you need to manipulate the thread priority.

3. ThreadPool

  • Use a ThreadPool thread when efficiency is desired. The ThreadPool helps avoid the overhead associated with creating, starting, and stopping threads.
  • Avoid using the ThreadPool if (1) the task runs for the lifetime of your application, (2) you need the thread to be a foreground thread, (3) you need to manipulate the thread priority, or (4) you need the thread to have a fixed identity (aborting, suspending, discovering).

4. Thread class

  • Use the Thread class for long-running tasks and when you require features offered by a formal threading model, e.g., choosing between foreground and background threads, tweaking the thread priority, fine-grained control over thread execution, etc.
claws
+1  A: 

I don't understand in .net why we have Asychronous calling (delegate.BeginInvoke() & delegate.EndInvoke()) as well as Thread class?

Generally the reason why - even in a very well-designed system - there are always going to multiple ways of doing one thing, is because some things are high-level facilities built out of low-level ones. If there is a high-level facility that suits your needs, it's your lucky day. If not, you have to build it yourself using the low-level facilities.

The Thread class uses Win32's BeginThread, so you don't have to. The thread pool uses the Thread class, so you don't have to. BeginInvoke uses the thread pool, so you don't have to, and so on.

Daniel Earwicker