And even the continuation task is assigned to another thread from the thread pool. The new task (generally) runs on a ThreadPool thread. When a new task is run, it is managed and synchronized by the CLR. Let’s look at a sample code from above and see how threads are involved here. So when we are working with Tasks, multiple threads are involved, though we do not generate them on our own. Task is an abstraction that, most of the times, uses some thread to run the operation asynchronously. To wait for multiple tasks to complete, use WaitAll(tasks). Also, if the task throws exception, that can be caught on the Wait(). Ideally, you’ll do other stuffs before waiting for the task. Just like above (sample) code, if we do a Wait() immediately after running a task, that’ll basically behave as a synchronous work. This actually halts the current thread and waits for the task to complete. In the DoAsyncWork_5() example we use a task.Wait() call. If we know there can be just one exception (we are very sure what all things that code is doing), we can simply use InnerExceptions. It has a collection of actual exceptions in InnerExceptions property. Note: The exception returned by task.Exception is of type AggregateException, that actually wraps one or more errors that occur during the task execution. Running some code on a separate threadįirst we’ll look at a very basic example of running a simple code on a new thread. But, that depends on the actual implementation of the task. Generally when using Task.Run() or similar constructs, a task runs on a separate thread (mostly a managed thread-pool one), managed by the. Note: Just using a Task in code does not mean there are separate new threads involved. NET abstraction that basically represents a promise of a separate work, that’ll be completed in future.Ī Task is nothing but a task that comes with promise of returning a value of type T when the task completes. Before making our choice, let’s understand what are threads and tasks.Ī thread is a low level construct and roughly represents an actual OS-level thread.Ī task is a. We’ll discuss async-await later, which is a comparatively newer concept. NET (framework version 4.0 and above) we have three high level options Here, we’ll use the term asynchronous only as that’ll be the our main intention. The concepts discussed below may apply to all three. (Which may involve threads running in the same or different core, or even in different systems!) Asynchronous means some operation is started out-of-sync in time, which will run on its own and notify when completed. threads in different processor cores running simultaneously). Parallel specifically means multiple things are being done actually at the same point of time (e.g. multiple threads sharing same processor core), which may or may not be parallel. Concurrent basically means multiple works being done in overlapping time (e.g. There are three different but very similar and related terms in computing: concurrent, parallel & asynchronous. Asynchronous, in general terms, means multiple things can run without depending on or waiting for each other, and can collaborate later if required. Well, in any of the modern tech platforms, we do use that technique to do stuffs asynchronously. With very basic idea of Operating systems and Programming, we can kind of guess that somehow we have to run the operation on a different thread while keeping the original thread free. How do I do other stuffs while that slow operation is running, rather than just sit idle and wait for it to complete. But that is a slow process and takes time. The problemīefore going into solution and available options, lets understand the problem that we are trying to address. NET application more efficient and better responsive to users.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |