Skip to main content
How are we doing? Please help us improve Stack Overflow. Take our short survey
402 votes
10 answers
80k views

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

Is there any scenario where writing method like this: public async Task<SomeResult> DoSomethingAsync() { // Some synchronous code might or might not be here... // return await ...
TX_'s user avatar
  • 5,446
161 votes
6 answers
102k views

'await' works, but calling task.Result hangs/deadlocks

I have the following four tests and the last one hangs when I run it. Why does this happen: [Test] public void CheckOnceResultTest() { Assert.IsTrue(CheckStatus().Result); } [Test] public async ...
Johan Larsson's user avatar
446 votes
2 answers
459k views

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 ...
Lukas K's user avatar
  • 6,349
421 votes
13 answers
224k views

Awaiting multiple Tasks with different results

I have 3 tasks: private async Task<Cat> FeedCat() {} private async Task<House> SellHouse() {} private async Task<Tesla> BuyCar() {} They all need to run before my code can continue ...
Ian Vink's user avatar
  • 68.7k
193 votes
4 answers
232k views

How to cancel a Task in await?

I'm playing with these Windows 8 WinRT tasks, and I'm trying to cancel a task using the method below, and it works to some point. The CancelNotification method DOES get called, which makes you think ...
Carlo's user avatar
  • 25.9k
445 votes
3 answers
409k views

Task vs Thread differences [duplicate]

There are two classes available in .NET: Task and Thread. What is the difference between those classes? When is it better to use Thread over Task (and vice-versa)?
Jacek's user avatar
  • 12k
166 votes
13 answers
211k views

How do I abort/cancel TPL Tasks?

In a thread, I create some System.Threading.Task and start each task. When I do a .Abort() to kill the thread, the tasks are not aborted. How can I transmit the .Abort() to my tasks ?
Patrice Pezillier's user avatar
103 votes
4 answers
203k views

Platform.runLater and Task in JavaFX

I have been doing some research on this but I am still VERY confused to say the least. Can anyone give me a concrete example of when to use Task and when to use Platform.runLater(Runnable);? What ...
Marc Rasmussen's user avatar
136 votes
2 answers
73k views

Await on a completed task same as task.Result?

I'm currently reading "Concurrency in C# Cookbook" by Stephen Cleary, and I noticed the following technique: var completedTask = await Task.WhenAny(downloadTask, timeoutTask); if (completedTask ==...
julio.g's user avatar
  • 3,320
155 votes
16 answers
139k views

Run PHP Task Asynchronously

I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don't want to make the user wait for the ...
davr's user avatar
  • 19.1k
516 votes
13 answers
443k views

How to safely call an async method in C# without await

I have an async method which returns no data: public async Task MyAsyncMethod() { // do some stuff async, don't return any data } I'm calling this from another method which returns some data: ...
George Powell's user avatar
16 votes
1 answer
6k views

JavaFX2: Can I pause a background Task / Service?

I am trying to set up a background service that would perform bulk loading of transaction data from a csv file. This background service would be initiated from a menu item action mapped to a method in ...
Lambda II's user avatar
  • 173
64 votes
9 answers
117k views

Android: Cancel Async Task

I use an async task to upload an image and get some results. While uploading the image I see a progress dialog, written in onPreExecute() method like this: protected void onPreExecute() { ...
steliosf's user avatar
  • 3,869
130 votes
9 answers
127k views

How can I call an async method in Main?

public class test { public async Task Go() { await PrintAnswerToLife(); Console.WriteLine("done"); } public async Task PrintAnswerToLife() { int answer = ...
Larry's user avatar
  • 2,242
70 votes
11 answers
93k views

How to limit the maximum number of parallel tasks in C#

I have a collection of 1000 input message to process. I'm looping the input collection and starting the new task for each message to get processed. //Assume this messages collection contains 1000 ...
Mathiyazhagan's user avatar

15 30 50 per page
1
2 3 4 5
61