All Questions

Tagged with
Filter by
Sorted by
Tagged with
377 votes
3 answers
443k views

Do you have to put Task.Run in a method to make it async?

I'm trying to understand async await in the simplest form. I want to create a very simple method that adds two numbers for the sake of this example, granted, it's no processing time at all, it's just ...
Neal's user avatar
  • 9,539
221 votes
6 answers
98k views

What's the difference between Task.Start/Wait and Async/Await?

I may be missing something but what is the difference between doing: public void MyMethod() { Task t = Task.Factory.StartNew(DoSomethingThatTakesTime); t.Wait(); UpdateLabelToSayItsComplete(); }...
Jon's user avatar
  • 39.5k
212 votes
4 answers
163k views

Using 'async' in a console application in C# [duplicate]

I have this simple code: public static async Task<int> SumTwoOperationsAsync() { var firstTask = GetOperationOneAsync(); var secondTask = GetOperationTwoAsync(); return await ...
Royi Namir's user avatar
  • 147k
209 votes
11 answers
111k views

Is it possible to await an event instead of another async method?

In my C#/XAML metro app, there's a button which kicks off a long-running process. So, as recommended, I'm using async/await to make sure the UI thread doesn't get blocked: private async void ...
Max's user avatar
  • 9,577
190 votes
5 answers
119k views

Async/await vs BackgroundWorker

In the past few days I have tested the new features of .net 4.5 and c# 5. I like its new async/await features. Earlier I had used BackgroundWorker to handle longer processes in the background with ...
Tom's user avatar
  • 3,999
190 votes
4 answers
225k 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.8k
149 votes
6 answers
199k views

Run two async tasks in parallel and collect results in .NET 4.5

I've been trying for a while to get something I thought would be simple working with .NET 4.5 I want to fire off two long running tasks at same time and collect the results in in the best C# 4.5 (...
Simon_Weaver's user avatar
132 votes
3 answers
48k views

Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?

I recently created a simple application for testing the HTTP call throughput that can be generated in an asynchronous manner vs a classical multithreaded approach. The application is a able to ...
Florin Dumitrescu's user avatar
112 votes
2 answers
113k views

HttpClient.GetAsync with network credentials

I'm currently using HttpWebRequest to get a website. I'd like to use the await pattern, which is not given for HttpWebRequests. I found the class HttpClient, which seems to be the new Http worker ...
Jan K.'s user avatar
  • 2,582
87 votes
13 answers
137k views

How to 'await' raising an EventHandler event

Sometimes the event pattern is used to raise events in MVVM applications by or a child viewmodel to send a message to its parent viewmodel in a loosely coupled way like this. Parent ViewModel ...
Simon_Weaver's user avatar
85 votes
4 answers
39k views

What's the difference between InvokeAsync and BeginInvoke for WPF Dispatcher

I noticed in .NET 4.5 that the WPF Dispatcher had gotten a new set of methods to execute stuff on the Dispatcher's thread called InvokeAsync. Before, .NET 4.5 we had Invoke and BeginInvoke which ...
Isak Savo's user avatar
  • 35.5k
79 votes
1 answer
148k views

Await operator can only be used within an Async method [duplicate]

I'm trying to make a simple program to test the new .NET async functionality within Visual Studio 2012. I generally use BackgroundWorkers to run time-consuming code asynchronously, but sometimes it ...
William Thomas's user avatar
61 votes
4 answers
34k views

What are the differences between using ConfigureAwait(false) and Task.Run?

I understand that it's recommended to use ConfigureAwait(false) for awaits in library code so that subsequent code does not run in the caller's execution context, which could be a UI thread. I also ...
Sam's user avatar
  • 41.5k
57 votes
10 answers
56k views

awaitable Task based queue

I'm wondering if there exists an implementation/wrapper for ConcurrentQueue, similar to BlockingCollection where taking from the collection does not block, but is instead asynchronous and will cause ...
spender's user avatar
  • 119k
51 votes
6 answers
30k views

NetworkStream.ReadAsync with a cancellation token never cancels

Here the proof. Any idea what is wrong in this code ? [TestMethod] public void TestTest() { var tcp = new TcpClient() { ReceiveTimeout = 5000, SendTimeout = 20000 }; tcp....
Softlion's user avatar
  • 12.4k
51 votes
7 answers
18k views

Awaiting Asynchronous function inside FormClosing Event

I'm having a problem where I cannot await an asynchronous function inside of the FormClosing event which will determine whether the form close should continue. I have created a simple example that ...
Hagelt18's user avatar
  • 838
50 votes
6 answers
32k views

Task.Yield - real usages?

I've been reading about Task.Yield , And as a Javascript developer I can tell that's it's job is exactly the same as setTimeout(function (){...},0); in terms of letting the main single thread deal ...
Royi Namir's user avatar
  • 147k
45 votes
3 answers
28k views

Brief explanation of Async/Await in .Net 4.5

How does Asynchronous tasks (Async/Await) work in .Net 4.5? Some sample code: private async Task<bool> TestFunction() { var x = await DoesSomethingExists(); var y = await ...
Mayank's user avatar
  • 8,824
41 votes
3 answers
30k views

Creating an async method in .NET 4.0 that can be used with "await" in .NET 4.5

I have a .NET project that uses C# in .NET 4.0 and VS2010. What I would like to do is add some async overloads to my library to make doing async programming easier for users in .NET 4.5 with the ...
James Newton-King's user avatar
39 votes
1 answer
14k views

HttpClient.PostAsync knocks out the app with exit code 0

Everything was working today until it stopped... Below is the minimum source code (I'm using VS 2012 Update 1, .Net 4.5). When I run it, app exits upon calling client.PostAsync() and so it never ...
Maxim's user avatar
  • 992
39 votes
7 answers
18k views

OperationContext.Current is null after first await when using async/await in WCF service

I am using async/await pattern in .NET 4.5 to implement some service methods in WCF. Example service: Contract: [ServiceContract(Namespace = "http://async.test/")] public interface IAsyncTest { ...
mdonatas's user avatar
  • 1,770
36 votes
3 answers
40k views

Using Async Await keywords with Dapper

I want to use a micro-orm and decided to go with Dapper. But can't seem to find any mentions of it supporting the new async/await syntax. Async queries are important for me. Can someone provide a ...
Yaron Levi's user avatar
  • 12.7k
35 votes
2 answers
68k views

How to determine a 404 response status when using the HttpClient.GetAsync()

I am trying to determine the response returned by HttpClient's GetAsync method in the case of 404 errors using C# and .NET 4.5. At present I can only tell that an error has occurred rather than the ...
Gga's user avatar
  • 4,381
33 votes
2 answers
10k views

Custom awaitables for dummies

In Async/Await FAQ, Stephen Toub says: An awaitable is any type that exposes a GetAwaiter method which returns a valid awaiter. ... An awaiter is any type returned from an awaitable’s ...
Şafak Gür's user avatar
  • 7,185
30 votes
3 answers
16k views

How to use await on methods in interfaces

When implementing against an interface (because of mocking, remoting or similiar) using the await keyword and having an interface with methods returning Task<> : interface IFoo { Task<...
Thomas Zeman's user avatar
28 votes
2 answers
22k views

awaiting on an observable

So in the sad days of C# 4.0, I created the following "WorkflowExecutor" class that allowed asynchronous workflows in the GUI thread by hacking into IEnumerable's "yield return" continuations to wait ...
Dax Fohl's user avatar
  • 10.7k
28 votes
1 answer
26k views

Passing a task as parameter

I am not sure whether this is possible, so here me out: I have a sequence of action to perform multiple async Task MethodA(...) { // some code // a call to specific Async IO bound method ...
Goran's user avatar
  • 6,398
27 votes
2 answers
6k views

Task.WhenAny and Unobserved Exceptions

Let's say I have three tasks, a, b, and c. All three are guaranteed to throw an exception at a random time between 1 and 5 seconds. I then write the following code: await Task.WhenAny(a, b, c); This ...
David Pfeffer's user avatar
25 votes
3 answers
15k views

async/await and opening a FileStream?

I came across the following question when trying to determine if I was using the Stream methods such as ReadAsync and CopyToAsync correctly: C# 4.5 file read performance sync vs async In this ...
Alex Hope O'Connor's user avatar
25 votes
2 answers
33k views

How can I await an async method without an async modifier in this parent method?

I have a method that I want to await but I don't want to cause a domino effect thinking anything can call this calling method and await it. For example, I have this method: public bool Save(string ...
Neal's user avatar
  • 9,539
24 votes
2 answers
7k views

HttpContext.Current.Items after an Async operation

Consider the following ASP.NET Web API Delegating Handler: public class MyHandler : DelegatingHandler { protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage ...
Gavin Osborn's user avatar
  • 2,593
24 votes
3 answers
26k views

Task status changes to RanToCompletion if the Task await's something

The question describes the same problem found here - MSDN Developer Forum. The question does not have an accepted answer, neither any of the answers given can be applied to my case (hence a different ...
tomsseisums's user avatar
  • 13.3k
23 votes
3 answers
1k views

When to use the "await" keyword

I'm writing a web page, and it calls some web services. The calls looked like this: var Data1 = await WebService1.Call(); var Data2 = await WebService2.Call(); var Data3 = await WebService3.Call(); ...
Eric B's user avatar
  • 4,417
22 votes
4 answers
19k views

Why is await async so slow?

I finally got VS2012 and got a simple demo up and working to check out the potential performance boost of async and await, but to my dismay it is slower! Its possible I'm doing something wrong, but ...
ohmusama's user avatar
  • 4,209
22 votes
2 answers
32k views

Using async / await with DataReader ? ( without middle buffers!)

My goal is simple , I want to do Asynchronous I/O calls (using async await) - but : Without using DataFlow dependency ( like in this answer) Without middle buffers( not like this answer) The ...
Royi Namir's user avatar
  • 147k
20 votes
4 answers
19k views

Task.WaitAll not waiting for task to complete

While trying to figure out the new (maybe not so new now, but new to me, anyway) Task asynchronous programming in C#, I ran into a problem that took me a bit to figure out, and I'm not sure why. I ...
cjk84's user avatar
  • 357
20 votes
3 answers
6k views

How to attach CancellationTokenSource to DownloadStringTaskAsync method and cancel the async call?

I an creating a sample example to call link using WebClient using async and await method now I want to attach cancel async call functionality also. But I am not able to get CancellationTokenSource ...
Balraj Singh's user avatar
  • 3,441
19 votes
3 answers
4k views

Is LogicalOperationStack incompatible with async in .Net 4.5

Trace.CorrelationManager.LogicalOperationStack enables having nested logical operation identifiers where the most common case is logging (NDC). Should it still work with async-await? Here's a simple ...
i3arnon's user avatar
  • 115k
17 votes
3 answers
14k views

Why does Task.Delay() allow an infinite delay?

After my application froze I tracked down the cause to a thread waiting on a task created by Task.Delay() (or TaskEx.Delay() in .NET 4.0) for which it provided a computed TimeSpan that, due to a bug, ...
Allon Guralnek's user avatar
17 votes
2 answers
7k views

What difference does it make - running an 'async' action delegate with a Task.Run (vs default action delegate)?

I am trying to get my head around async/await and thought I did understand few things about the usage. But still not quite clear what would be the actual benefit in a scenario like below. Look at the ...
Everything Matters's user avatar
16 votes
3 answers
7k views

.NET 4 equivalent of Task.WhenAll()

In .NET 4, is there any functional equivalent to .NET 4.5's System.Threading.Tasks.Task.WhenAll()? The goal is to wrap up multiple async tasks into a single one that is completed when all of its ...
kpozin's user avatar
  • 26.4k
16 votes
3 answers
9k views

MVC4 + async/await + return response before action completes

In my MVC4 app I need to add a controller for uploading and processing large files. Immediately after the file is uploaded I need to start async processing of that file and return response to the ...
filip's user avatar
  • 1,475
16 votes
2 answers
12k views

How do I kick off an entity stored procedure in EF6 async and not wait for a return?

I'd like to just punt a call over to the SQL Server and not wait for a return. I have an imported Entity Function from a Stored Procedure that I'd like to call asynchronously this way in Entity ...
dirq's user avatar
  • 978
16 votes
3 answers
14k views

C# have async function call synchronous function or synchronous function call async function

I'm writing a C# .Net 4.5 library for doing common sql database operations (backup, restore, execute script, etc.). I want to have both synchronous and asynchronous functions for each operation, as ...
deadlydog's user avatar
  • 23.4k
15 votes
1 answer
33k views

The 'await' operator can only be used with an async lambda expression [duplicate]

I'm trying to copy a list of files to a directory. I'm using async / await. But I've been getting this compilation error The 'await' operator can only be used within an async lambda expression. ...
abhilash's user avatar
  • 5,625
15 votes
1 answer
10k views

Why do I have to use await for a method to run asynchronously. What if I don't want to wait for the method to finish before continuing? [duplicate]

I've been pouring through MSDN docs all day, and their philosophy of asynchronous coding is confusing me. As I understand it, the thread that calls the async method will not be blocked if the async ...
thefistopher's user avatar
15 votes
1 answer
10k views

.Net 4.5 Svcutil generates two operations with the same name (Method and MethodAsync)

I am consuming a predefined wsdl with svcutil a la: svcutil some_service.wsdl one of the methods generated has the following signature: [System.ServiceModel.OperationContractAttribute(Action="http:/...
vossad01's user avatar
  • 11.8k
15 votes
2 answers
9k views

Does Task.Delay start a new thread?

The following code should (at least in my opinion) create 100 Tasks, which are all waiting in parallel (that's the point about concurrency, right :D ?) and finish almost at the same time. I guess for ...
Kr0e's user avatar
  • 2,199
15 votes
4 answers
2k views

Code Contracts + Async in .NET 4.5: "The method or operation is not implemented"

I receive the following compilation error from ccrewrite when using Code Contracts 1.4.51019.0 in VS2012 on Windows 7 x64: "The method or operation is not implemented." It appears to be caused by a ...
Lawrence Wagerfield's user avatar
14 votes
3 answers
12k views

Where does an async Task throw Exception if it is not awaited?

I have the following example: (please also read comments in code, as it will make more sense ) public async Task<Task<Result>> MyAsyncMethod() { Task<Result> resultTask = ...
Dan Dinu's user avatar
  • 33k

1
2 3 4 5