All Questions

Tagged with
Filter by
Sorted by
Tagged with
722 votes
6 answers
587k views

async/await - when to return a Task vs void?

Under what scenarios would one want to use public async Task AsyncMethod(int num) instead of public async void AsyncMethod(int num) The only scenario that I can think of is if you need the task ...
user981225's user avatar
  • 9,032
155 votes
2 answers
168k views

How does Task<int> become an int?

We have this method: async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); ...
Freeman's user avatar
  • 5,741
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
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
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
32 votes
2 answers
24k views

.NET 4.5 file read performance sync vs async

We're trying to measure the performance between reading a series of files using sync methods vs async. Was expecting to have about the same time between the two but turns out using async is about 5.5x ...
gcastelo's user avatar
  • 403
32 votes
2 answers
45k views

"await" doesn't wait for the completion of call

I'm building a Metro App. In the MainPage.xaml.cs, I instantiate Album as follows: Album album = new Album(2012); //With the album ID as its parameter. ListView1.ItemsSource = album.Songs; In the ...
Isilmë O.'s user avatar
  • 1,718
30 votes
5 answers
59k views

HttpClient is not found in .NET 4.5

I am trying to use the new HttpClient in .NET 4.5, but Visual Studio complains that it doesn't exist. I have System.Net, but when I type System.Net.Http, it complains for that too. Am I supposed to ...
Joan Venge's user avatar
  • 323k
30 votes
4 answers
8k views

.NET 4.5 async await and overloaded methods

I have an async method: public async Task<UserLoginExResult> LoginExAsync(CustomTable exRequest, string language, bool throwEx = true) { UserLoginExResult result = await UserService....
JakubRi's user avatar
  • 829
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
24 votes
1 answer
6k views

what is the main difference between .net Async and google go light weight thread

When calling runtime.GOMAXPROCS(1) in go the runtime will only use one thread for all your goroutines. When doing io your goroutines will yield and let the other goroutines run on the same thread. ...
skyde's user avatar
  • 2,876
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
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
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
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
12 votes
4 answers
13k views

Cancel C# 4.5 TcpClient ReadAsync by timeout

What would be the proper way to cancel TcpClient ReadAsync operation by timeout and catch this timeout event in .NET 4.5? TcpClient.ReadTimeout seems to be applied to the sync Read only. UPDATE: Tried ...
miksh's user avatar
  • 189
12 votes
3 answers
21k views

How to use Task.WhenAll() correctly

I am trying to use Task.WhenAll to await completion of multiple tasks. My code is below - it is supposed to launch multiple async tasks, each of which retrieves a bus route and then adds them to a ...
Carlos P's user avatar
  • 3,948
12 votes
2 answers
8k views

Dangling await and possible memory leaks in async programming

The flow containing await in .NET 4.5 and Async CTP 4.0 can be stuck due to various reasons, e.g. since the remote client has not responded. Of course, WaitForAny, when we wait also for some timeout ...
Yuri S.'s user avatar
  • 283
10 votes
1 answer
5k views

Exception handling in fire and forget for C# 5 (in .net 4.5)

Consider the following "fire-and-forget" use case: A caller requests some data from my method. My method checks the cache to see if the data is already there. If it's not, it fetches it from the ...
Bill Sourour's user avatar
  • 1,188
8 votes
3 answers
1k views

C# Concurrency - Preferred approach to long running tasks

When there needs to be an I/O listener running throughout an applications lifetime, what concurrency model is preferred with C# 5.0 running on the 4.5 framework? I've settled on the idea that a ...
BlackBox's user avatar
  • 2,223
7 votes
3 answers
6k views

async/await. Where is continuation of awaitable part of method performed?

I am really curious how async/await enables your program not to be halted. I really like the way how Stephen Cleary explains async/await: "I like to think of "await" as an "asynchronous wait". That is ...
StepUp's user avatar
  • 37.3k
7 votes
2 answers
3k views

Call Async Method from Session_Start

How can I call an async method from Session_Start in Global.asax ? Global.asax: protected async Task Session_Start(object sender, EventArgs e) { Session.Timeout = 10; ...
Danielle's user avatar
  • 3,622
7 votes
1 answer
4k views

WCF / svcutil in .NET 4.5 generates unusable code by default

With .NET 4.5, my WCF creation using svcutil suddenly seems to break (I've been using only .NET 4.0 until very recently) .... With the default settings I'm using to convert a pre-existing WSDL to my ...
marc_s's user avatar
  • 743k
7 votes
3 answers
4k views

How to unit test RelayCommand that Executes an async method?

As there is no RelayCommandAsync (at least not that I know of), how to test this scenario. For example: public RelayCommand LoadJobCommand { get { return this.loadJobCommand ?? ( ...
O.O's user avatar
  • 11.2k
6 votes
4 answers
12k views

How do I create a Task that uses await inside the body that behaves the same as the synchronous version when Wait is called?

I have some code that creates a task that does some slow work like this: public static Task wait1() { return new Task(() => { Console.WriteLine("Waiting..."); Thread.Sleep(...
Danny Tuppeny's user avatar
6 votes
1 answer
7k views

Async friendly DispatcherTimer wrapper/subclass

I have a DispatcherTimer running in my code that fire every 30 seconds to update system status from the server. The timer fires in the client even if I'm debugging my server code so if I've been ...
Simon_Weaver's user avatar
6 votes
2 answers
7k views

Async task does not end

I'm trying to start async task (on .NET 4.5) which downloads content of web page, but somehow this task never finishes. My PageDownloader class: using System.Net; using System.Text; using System.IO; ...
Yury Pogrebnyak's user avatar
6 votes
2 answers
3k views

Task.ContinueWith confusion

With ASP.NET 4.5 I'm trying to play with the new async/await toys. I have a IDataReader-implementing class that wraps a vendor-specific reader (like SqlDatareader). I have a simple ExecuteSql() method ...
n8wrl's user avatar
  • 19.6k
6 votes
5 answers
4k views

Binding and Async Operations

I have created a Window with a TextBlock inside. I have bound the Text property and everything works fine. BUT When I change the bounded property while inside a Task then nothing works!! Do you know ...
Emmanouil Chountasis's user avatar
6 votes
1 answer
3k views

Should I upgrade to be able to use the new async methods for SqlDataReader?

I'm working on a large project that runs on .NET 4.0. This framework uses ADO.NET for database calls and we're currently adding asynchronous API methods. The SqlCommand class has the APM methods ...
annemartijn's user avatar
  • 1,596
6 votes
2 answers
636 views

parallel event subscriber .net 4.5

I am trying to create a parallel event subscriber. This is my first attempt: using System; using System.Collections.Generic; using System.Threading.Tasks; using EventStore.ClientAPI; namespace ...
cs0815's user avatar
  • 17.1k
6 votes
1 answer
5k views

Much memory fragmentation with System.Threading.OverlappedData (async await)

I'm constantly running slowly out of memory. I've used WinDbg to have a look at the memory dump and this it what it looks like: 000007f975ee0630 557377 41027736 System.Object[] 000007f975f004d0 ...
user1275154's user avatar
  • 1,130
5 votes
2 answers
7k views

HttpClient (C#) fails on many asynchronous requests?

I'm using HttpClient to asynchronously make many requests to an external api. I wait for all the requests to complete, then use the responses in other code. My problem is that if I make too many ...
user2503038's user avatar
5 votes
1 answer
5k views

Async method in Global.asax

I have to call some async methods in the Application_PostAcquireRequestState method of my Global.asax (these methods comes from a library and there's no equivalent sync method for these operations). I ...
mberube.Net's user avatar
  • 2,140
5 votes
1 answer
2k views

Task continuations always run even when specifying TaskContinuationOptions

I want to run some code when an async task completes successfully. From reading documentation and examples on the web, I thought I could use Task.ContinueWith and specify TaskContinuationOptions....
corvuscorax's user avatar
  • 5,890
5 votes
4 answers
2k views

How to make Sequential Processing as simple as Parallel Processing

I've got two .net Task objects that I may wish to run in parellel or in sequence. In either case, I don't want to block a thread to wait for them. As it turns out, Reactive Extensions make the ...
Brent Arias's user avatar
  • 29.7k
5 votes
2 answers
2k views

C# async file transfer - waiting before continuing loop

I am trying to get my head around the changes in .NET 4.5, mainly the async features. To get my head around it I thought i would create a little app for archiving my massive photo collection. I learn ...
Lotok's user avatar
  • 4,577
5 votes
1 answer
2k views

Running external processes asynchronously in a windows service

I am writing a program that moves csv files from a "queue" folder to a "processing" folder, then a third party process called import.exe is launched taking the csv file path as an argument. Import.exe ...
SeanOB's user avatar
  • 772
4 votes
2 answers
6k views

return Task.ContinueWith<TResult>() without knowing TResult

How do I create and return a continuation Task using reflection or by any other means if I only have access the Task? I need a way to let an exception in the continuation travel back to the original ...
Tersius's user avatar
  • 267
4 votes
1 answer
960 views

BeginExecuteReader, EndExecuteReader and multiple results

I have method that is asynchronously getting results form my database: internal class CommandAndCallback<TCallback, TError> { public SqlCommand Sql { get; set; } public TCallback ...
Misiu's user avatar
  • 4,857
4 votes
2 answers
1k views

Async design pattern - which one is better? [closed]

I've started wondering for asynchronous programming should I always use following pattern: public async Task<int> MyMethodAsync() { return await SomeOtherMethodAsync(); } or is it safe and ...
Marcin's user avatar
  • 3,252
4 votes
1 answer
3k views

Calling async method and TaskScheduler in async/await

Consider the following code async Task<int> foo() { await Task.Delay(1000); return 42; } ... // OPTION 1 Task t = Task.Factory.StartNew(foo, CancellationToken.None, ...
Boris's user avatar
  • 1,333
4 votes
1 answer
2k views

Why doesn't my Task.ContinueWith execute in .NET 4.5?

Consider the following code Task<T>.Factory.StartNew(() => { // block #1: load some data from local file cache } ) .ContinueWith(task => { // block #2: ...
Dan Sinclair's user avatar
3 votes
2 answers
2k views

create an async method that wraps subscribe and publish on a bus

I have a kind of bus that implements this interface: public interface IBus { void Publish<T>(T t); void Subscribe<T>(Guid subscriptionId, Action<T> action); void ...
Alon's user avatar
  • 353
3 votes
2 answers
8k views

How do I create a callback in C# (.NET 4.5) for HttpClient.GetAsync(URI)?

I'd like to create a simple, async request to Google search. According to Google, the simplest way to do this is using their JSON API with the simple curl request curl -e http://www.my-ajax-site.com ...
Steen Schütt's user avatar
3 votes
2 answers
5k views

How to implement Command Pattern with async/await

i'm currently upgrading some existing code for use by windows universal and am struggling to convert a command pattern to work with the new async/await functionality. I have a command scheduler class ...
Daz Eddy's user avatar
  • 342
3 votes
1 answer
4k views

C#5 async method completed event.

I have an asynchronous method like this public async void Method() { await // Long run method } When Im calling this method can I have a event when this Method completed? public void CallMethod(...
Sency's user avatar
  • 2,868
3 votes
1 answer
2k views

Unresponsiveness with async event handlers in WPF in .NET 4.5

I have created a simple async operation which is being kicked of when the button is clicked. Here is the whole code: public partial class MainWindow : Window { public MainWindow() { ...
tugberk's user avatar
  • 58.1k
3 votes
1 answer
1k views

Wait for IProgress Report Method to Return

I am updating my UI via IProgress and the Report method. I do something like IProgress<ProgressInfo> p = new Progress<ProgressInfo>(ReportProgress); Task task = Task.Factory.StartNew(() =...
MoonKnight's user avatar
  • 23.5k