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
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
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
69 votes
6 answers
11k views

Why doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?

In .NET 4.5 / C# 5, IReadOnlyCollection<T> is declared with a Count property: public interface IReadOnlyCollection<out T> : IEnumerable<T>, IEnumerable { int Count { get; } } I ...
Zaid Masud's user avatar
  • 13.3k
63 votes
1 answer
26k views

What can I do in C# 5 with .Net 4.5 that I couldn't do in C# 4 with .Net 4? [closed]

I have Visual Studio 2012 RC installed on Windows 8 Release Preview and my question is are there any useful new features not related to Metro, or is Metro what seperates .Net 4 and .Net 4.5?
JMK's user avatar
  • 27.7k
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
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
40 votes
1 answer
7k views

Has foreach's use of variables been changed in C# 5?

In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that "FYI we are highly likely to fix this in the next version of C#; this is a major pain point for developers" with regards ...
a_hardin's user avatar
  • 5,122
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
23 votes
8 answers
30k views

TcpListener: how to stop listening while awaiting AcceptTcpClientAsync()?

I don't know how to properly close a TcpListener while an async method await for incoming connections. I found this code on SO, here the code : public class Server { private TcpListener _Server; ...
Bastiflew's user avatar
  • 1,146
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
5 answers
344 views

Is there an elegant LINQ solution for SomeButNotAll()?

Here is what I'm trying to do overall. Just to be clear, this isn't homework or for a contest or anything. Hopefully, I've made the wording clear enough: Problem Given a set of strings in the same ...
chris's user avatar
  • 61.3k
18 votes
2 answers
12k views

Building .NET 4.5 Projects with Nant

I'm curious if it's possible to use Nant to target the .NET 4.5 using the C# 5.0 compiler. As of right now, the latest version only states support for .NET 4.0. I tried downloading the source and ...
Joel Martinez's user avatar
18 votes
1 answer
12k views

Interface implementation with optional arguments

Take this interface: interface ILogger { void Store(string payload); } And this class implementation of ILogger: class Logger : ILogger { void Store(string payload, bool swallowException = ...
James Jones's user avatar
  • 8,683
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
13 votes
1 answer
5k views

How to use a breakpoint after await in unit test?

I am surprised my breakpoint after awaiting an async method that is on a line that references the awaited Task<T> is never hit: [Test] public async void GetSomethingTest() { ...
markmnl's user avatar
  • 11.3k
13 votes
5 answers
6k views

How can I start a Windows App Background Task immediately after registering it?

I am writing a Metro App that will only run on PCs (so there is no, or at least less, worry about the battery life). I need it to register and run a background task when the user clicks a button. It ...
Garrett's user avatar
  • 5,620
11 votes
2 answers
6k views

How do I convert this to an async task?

Given the following code... static void DoSomething(int id) { Thread.Sleep(50); Console.WriteLine(@"DidSomething({0})", id); } I know I can convert this to an async task as follows... ...
Martin Robins's user avatar
10 votes
3 answers
8k views

async - stay on the current thread?

I've read Eric lippert's article about async , and about confusions people had with async keyword. he said : it (async) means “this method contains control flow that involves awaiting ...
Royi Namir's user avatar
  • 147k
9 votes
1 answer
2k views

c# 5 async as a sugar syntax (or not)?

So I was asking this question about async , and I thought that it it's just a sugar syntax for : Task<..>...ContinueWith... And finally inspect the Result property. I even asked a question ...
Royi Namir's user avatar
  • 147k
9 votes
2 answers
22k views

.NET 4.5 forms app connecting to SQL Server 2012 fails: SSL Provider, error: 0

I have a problem connection to a SQL Server 2012 instance running on Windows Server 2012. I have a .NET 4.5 windows forms application installed on a client machine running Windows 7. The error I get ...
Pelle's user avatar
  • 2,775
7 votes
2 answers
2k views

async void event handlers - clarification?

I'm trying to understand the reason why is it bad to do: (notice, context here is asp.net, regardless the plain reason that async void can't be tracked) public async void Page_Load(object sender, ...
Royi Namir's user avatar
  • 147k
6 votes
2 answers
15k views

C# 5.0 async await return a list

I'm learning about async/await, and ran into a situation where I need to call an async method that should return an object or list of same object. Is this the right way to implement ? from AManager....
ilansch's user avatar
  • 4,834
6 votes
2 answers
2k views

What in the Rx Framework allows me to return an IObservable<T> while awaiting other methods during creation?

I've been working on creating an IObservable<T> implementation using the Reactive Extensions for Twitter's streaming APIs. From a high level an HTTP request is sent and the connection is kept ...
casperOne's user avatar
  • 74.1k
6 votes
4 answers
7k views

Most efficient way of reading a BinaryFormatter serialized object from a NetworkStream?

I have an app that is sending serializable objects of varying sizes over a socket connection, and I'd like it to be as scalable as possible. There could also be dozens to even hundreds of connections. ...
Ben Lesh's user avatar
  • 108k
6 votes
1 answer
341 views

Why await both the asynchronous request and the reading of its contents?

Why is it necessary in .NET Web API to have a a method that reads the content of an HTTP response asynchronously, given that there is already a method to make the request asynchronously? Said another ...
Keith Pinson's user avatar
  • 7,875
6 votes
1 answer
2k views

Why does cancellation block for so long when cancelling a lot of HTTP requests?

Background I have some code that performs batch HTML page processing using content from one specific host. It tries to make a large number (~400) of simultaneous HTTP requests using HttpClient. I ...
Sam's user avatar
  • 41.5k
5 votes
2 answers
2k views

RESTful authentication. Client-side, stateless unauthentication

I'm implementing a set of RESTful services for some developments and one of these is an authentication service. This authentication service authenticates two kinds of identities: Applications. ...
Matías Fidemraizer's user avatar
5 votes
1 answer
1k views

Easy Way to Perform C# Null Check in a Type Conversion

I am doing some quick type conversions in a project I am not that very familiar with. They look similar to this: var NewType = new { NewTypeId = old.SubType == null ? 0 : old.SubType.SubTypeId ?...
AnotherDeveloper's user avatar
5 votes
2 answers
311 views

Cancelling all tasks when a new one is created

I have the following task that is run when the section item of a list box changes. I am trying to cancel any task that are running when a user changes their selection and start a new task . I can ...
justinf's user avatar
  • 1,268
4 votes
4 answers
4k views

What is the best way to return completed Task?

What is the best way to return a completed Task object? It is possible to write Task.Delay(0), or Task.FromResult<bool>(true) whatever. But what is the most efficient way?
XperiAndri's user avatar
  • 1,140
4 votes
3 answers
3k views

async await usages for CPU computing vs IO operation?

I already know that async-await keeps the thread context , also handle exception forwarding etc.(which helps a lot). But consider the following example : /*1*/ public async Task<int> ...
Royi Namir's user avatar
  • 147k
4 votes
1 answer
2k views

Cold Tasks and TaskExtensions.Unwrap

I've got a caching class that uses cold (unstarted) tasks to avoid running the expensive thing multiple times. public class AsyncConcurrentDictionary<TKey, TValue> : System.Collections....
Fowl's user avatar
  • 5,038
4 votes
3 answers
916 views

Why does MVC 4 dispose the controller when action await something?

Why is my controller being disposed after the await statement? public async Task<ViewResult> MyAction() { await Task.Yield(); // controller disposed at this ...
Miguel Angelo's user avatar
4 votes
0 answers
2k views

How to await Tasks kicked off in Dispose

I have a general question about how to keep track of an asynchronous task that is started when an object is disposed in .Net. I have a class designed around Stephen Cleary's Dispose means cancel ...
Glen Nicol's user avatar
3 votes
2 answers
5k views

C# async awaitable clarification?

I've read here that : Await examines that awaitable to see if it has already completed; if the awaitable has already completed, then the method just continues running (synchronously, just like ...
Royi Namir's user avatar
  • 147k
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
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
2 answers
1k views

How to use async/await with a library that uses an event-based asynchronous pattern?

I use a library that has an asynchronous method called DoWork(...) that will raise a WorkDone event when the operation completes. I would like to write a method that calls this library, but instead ...
Xavier Poinas's user avatar
3 votes
1 answer
243 views

WEB API call stops working after introducing the "await" keyword

I have a simple web api method which retrieves a list of items from a database. When the method looks like this it works and the items are displayed in the UI: [ActionName("GetServices")] public ...
Denys Wessels's user avatar
2 votes
1 answer
4k views

Getting deadlock when using ExecuteReaderAsync

I'm trying to use the async pattern to execute a SQL command and return a DataTable. Can someone please advice how to solve this problem? This is my code: private static async Task<DataTable&...
thomas's user avatar
  • 1,449
2 votes
2 answers
341 views

Examples of solutions using C# 5 Caller Information

I am struggling to think of real-world usage of C# 5's Caller Information feature In what scenarios do you need to know who called your method? What other usages does it have other than tracing and ...
Sam Leach's user avatar
  • 12.8k
2 votes
1 answer
515 views

Overload resolution and async await

Below code gives error CS0121, The call is ambiguous between the following methods or properties: 'RunTask(System.Func<System.Threading.Tasks.Task>)' and 'RunTask(System.Action)' static ...
imgen's user avatar
  • 2,921
2 votes
1 answer
215 views

async ( with non-async) function control flow clarification?

( I've read a lot about async and I wonder what happens if there is a mix of async function call and non-async) and im talking about THREAD pov . ( I know that mix should not be done , but im asking ...
Royi Namir's user avatar
  • 147k
2 votes
1 answer
1k views

Windows Phone 8 MVVM with JSON Deserialization

(Need suggestion about scary code) I am trying to bring about some elegance to my old Windows Phone app code by incorporating MVVM and a generalized DataService class for all View Models, to call ...
vulcan raven's user avatar
  • 33.1k
2 votes
1 answer
638 views

ReactiveAsyncCommand missing in ReactiveUI 5.0.2

I just start learn ReactiveUI from https://github.com/reactiveui/ReactiveUI.Samples/blob/master/ReactiveUI_4Only.Samples.sln. I download lastest version via nuget but I cant find class ...
Kopernik's user avatar
2 votes
1 answer
417 views

How to improve throughput with FileStream in a single-threaded application

I am trying to get top I/O performance in a data streaming application with eight SSDs in RAID-5 (each SSD advertises and delivers 500 MB/sec reads). I create FileStream with 64KB buffer and read ...
GregC's user avatar
  • 7,807
2 votes
1 answer
790 views

Unit testing async method: The remote hostanme could not be resolved

I am trying to test some async methods involving the HttpClient work with vs 2012 express for win 8 and it's test framework. No matter how I try I always get the same exception. For example this test ...
Alexander Nyrkov's user avatar
2 votes
0 answers
315 views

Equivalence or conversion matrix between .NET 4.0 Async CTP terms and their .NET 4.5 analogs?

I am using Async CTP (Version 3) + SP1 to it for developing and testing in Visual Studio 2010 SP1 on Windows XP SP3 mainly because my clients (as well as I) are on Windows XP SP3. See related topics: ...
Gennady Vanin Геннадий Ванин's user avatar
1 vote
1 answer
427 views

How to get data of the Task with await keyword?

I just need in main code get FlowDocument... I do like this but no success this.flowDoc = engine.CreateFlowDocument(contentItems); // Can't compile public async Task<FlowDocument> ...
NoWar's user avatar
  • 37k