Questions tagged [asynchronous]

Asynchronous programming is a strategy for deferring operations with high latency or low priority, usually in an attempt to improve performance, responsiveness, and / or composability of software. Such strategies are usually employed using some combination of event-driven programming and callbacks, and optionally making use of concurrency through coroutines and / or threads.

Asynchronous programming is a strategy for deferring operations with high latency or low priority, usually in an attempt to improve performance, responsiveness, and / or composability of software. Such strategies are usually employed using some combination of event-driven programming and callbacks, and optionally making use of concurrency through coroutines and / or threads.

Asynchronous programming is used in many situations:

  • handling user input in UIs and games,
  • processing network traffic,
  • performing disk I/O,
  • batching work,
  • and more.

Asynchronous programming models can aid in software composition in many languages (notably languages where functions are first-class types) and APIs providing callback-based completion messaging. Proper use of this programming methodology can improve throughput and improve responsiveness by reducing total latency of job batches when executed in parallel. This approach can also result in an increase in system throughput at the cost of increased operational latency resulting from deferred processing.

50995 questions
483
votes
16 answers

asynchronous and non-blocking calls? also between blocking and synchronous

What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)?
user331561
  • 4,921
  • 3
  • 16
  • 5
464
votes
8 answers

How to reject in async/await syntax?

How can I reject a promise that returned by an async/await function? e.g. Originally: foo(id: string): Promise { return new Promise((resolve, reject) => { someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400)) …
Phoenix
  • 4,773
  • 2
  • 10
  • 6
439
votes
2 answers

What is the difference between asynchronous programming and multithreading?

I thought that they were basically the same thing — writing programs that split tasks between processors (on machines that have 2+ processors). Then I'm reading this, which says: Async methods are intended to be non-blocking operations. An…
437
votes
12 answers

JavaScript, Node.js: is Array.forEach asynchronous?

I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave asynchronously? For example, if I call: [many many elements].forEach(function () {lots of work to do}) Will this be non-blocking?
R. Gr.
  • 4,411
  • 2
  • 15
  • 4
426
votes
2 answers

When correctly use Task.Run and when just async-await

I would like to ask you on your opinion about the correct architecture when to use Task.Run. I am experiencing laggy UI in our WPF .NET 4.5 application (with Caliburn Micro framework). Basically I am doing (very simplified code snippets): public…
Lukas K
  • 6,037
  • 4
  • 23
  • 31
417
votes
4 answers

Sleep Command in T-SQL?

Is there to way write a T-SQL command to just make it sleep for a period of time? I am writing a web service asynchronously and I want to be able to run some tests to see if the asynchronous pattern is really going to make it more scalable. In…
skb
  • 30,624
  • 33
  • 94
  • 146
387
votes
12 answers

Awaiting multiple Tasks with different results

I have 3 tasks: private async Task FeedCat() {} private async Task SellHouse() {} private async Task BuyCar() {} They all need to run before my code can continue and I need the results from each as well. None of the results have…
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
384
votes
11 answers

Running multiple async tasks and waiting for them all to complete

I need to run multiple async tasks in a console application, and wait for them all to complete before further processing. There's many articles out there, but I seem to get more confused the more I read. I've read and understand the basic principles…
Daniel Minnaar
  • 5,865
  • 5
  • 31
  • 52
371
votes
5 answers

How can I limit Parallel.ForEach?

I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth is limited so I can download only x pages per time but Parallel.ForEach executes whole list of desired webpages. Is there a way to limit thread number or any…
eugeneK
  • 10,750
  • 19
  • 66
  • 101
371
votes
7 answers

HttpClient.GetAsync(...) never returns when using await/async

Edit: This question looks like it might be the same problem, but has no responses... Edit: In test case 5 the task appears to be stuck in WaitingForActivation state. I've encountered some odd behaviour using the System.Net.Http.HttpClient in .NET…
Benjamin Fox
  • 5,624
  • 5
  • 19
  • 19
366
votes
11 answers

If async-await doesn't create any additional threads, then how does it make applications responsive?

Time and time again, I see it said that using async-await doesn't create any additional threads. That doesn't make sense because the only ways that a computer can appear to be doing more than 1 thing at a time is Actually doing more than 1 thing at…
Ms. Corlib
  • 4,993
  • 4
  • 12
  • 19
365
votes
9 answers

Why use async and return await, when you can return Task directly?

Is there any scenario where writing method like this: public async Task DoSomethingAsync() { // Some synchronous code might or might not be here... // return await DoAnotherThingAsync(); } instead of this: public…
TX_
  • 5,056
  • 3
  • 28
  • 40
354
votes
6 answers

Catch an exception thrown by an async void method

Using the async CTP from Microsoft for .NET, is it possible to catch an exception thrown by an async method in the calling method? public async void Foo() { var x = await DoSomethingAsync(); /* Handle the result, but sometimes an exception…
TimothyP
  • 21,178
  • 26
  • 94
  • 142
328
votes
8 answers

Async await in linq select

I need to modify an existing program and it contains following code: var inputs = events.Select(async ev => await ProcessEventAsync(ev)) .Select(t => t.Result) .Where(i => i != null) …
Alexander Derck
  • 13,818
  • 5
  • 54
  • 76
324
votes
17 answers

What is the difference between concurrency, parallelism and asynchronous methods?

Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in parallel but on the same 1 thread. How is this achieved? Also, what about parallelism? What are the differences between these 3 concepts?
GurdeepS
  • 65,107
  • 109
  • 251
  • 387