Asynchronous Delegates Vs Thread/ThreadPool?

前端 未结 8 2137
醉梦人生
醉梦人生 2020-12-22 23:57

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

相关标签:
8条回答
  • 2020-12-23 00:24

    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.

    BackgroundWorker vs background Thread

    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.
    0 讨论(0)
  • 2020-12-23 00:30

    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.

    0 讨论(0)
  • 2020-12-23 00:30

    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.

    0 讨论(0)
  • 2020-12-23 00:31

    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.

    0 讨论(0)
  • 2020-12-23 00:33

    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.

    0 讨论(0)
  • 2020-12-23 00:42

    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.

    0 讨论(0)
提交回复
热议问题