c#-5.0

How to correctly write async method?

為{幸葍}努か 提交于 2019-12-10 21:52:42
问题 So I am trying to learn the basics of using 'async' and 'await' in C#, but I am not sure what I am doing wrong here. I am expecting the following output: Calling DoDownload DoDownload done [...output here...] But I don't get the output of the download, and I also expect "done" but that takes a while. Shouldn't that be output immediately? Also, I can't seem to get the string result either. Here is my code: namespace AsyncTest { class Program { static void Main(string[] args) { Debug.WriteLine(

async function over a list

女生的网名这么多〃 提交于 2019-12-10 13:47:56
问题 I Have a function that looks like this: public async Task<decimal> GoToWeb(string Sym){} what's the best way to call it over a list of strings? 回答1: Here's an article from MSDN on using async-await to process multilpe tasks in parallel. And here's another that specifically addresses a collection of tasks. In short, you can do one of the following: Start all of your tasks and then await each of them. They will all run in parallel and your program will continue once they all complete. Put your

Why DbSet<TEntity> doesn't implement EnumerableAsync

喜欢而已 提交于 2019-12-10 12:39:03
问题 In Entity framework 6.1.1 an IDbSet represents the collection of entities which can be queried from the database and its concrete implementation is DbSet as described in DbSet How come this interface or its concrete implementation doesn't contain any definition for ToEnumerableAsync or AsEnumerableAsync but ToListAsync,ToArrayAsync,ToDictionaryAsync? To give you an idea of why I came across this question I have the following piece of code which I wanted to make async: public IQueryable

Async method not returning immediately [duplicate]

倖福魔咒の 提交于 2019-12-10 11:44:58
问题 This question already has an answer here : UI Thread blocked in C# WPF App during Async Download (1 answer) Closed 5 years ago . I am working from this solution: How to correctly write async method? However, the async method does not seem to return immediately, rather it takes a while. Here is the class Program { static void Main(string[] args) { Debug.WriteLine("Calling DoDownload"); var downloadTask = DoDownloadAsync(); Debug.WriteLine("DoDownload done"); downloadTask.Wait(); //Waits for

AZURE: async Run() in workerrole

旧巷老猫 提交于 2019-12-10 11:25:36
问题 I have an async task. async Task UploadFiles() { } I would like to call 'await' on UploadFiles() in Run() method in azure workerrole. but 'await' works only in the methods declared async. So can I make Run() method async like below: public override void Run() { UploadFiles(); } to public async override void Run() { await UploadFiles(); } 回答1: Worker roles only have a synchronous entry point. This means that you will need to keep the thread that the Run method runs on active. You can just call

What is the best way to return completed Task?

谁说我不能喝 提交于 2019-12-10 10:59:51
问题 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? 回答1: Answer from Stephen Toub (MSFT): If you want a new Task object each time, Task.FromResult is the most efficient. Task.Delay(0) in its current implementation will return a cached task, but that's an implementation detail. If you want to use a cached task, you should cache one yourself, e.g. private static readonly Task

async await usages for CPU computing vs IO operation?

好久不见. 提交于 2019-12-10 09:25:25
问题 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> ExampleMethodAsync() /*2*/ { /*3*/ var httpClient = new HttpClient(); /*4*/ /*5*/ //start async task... /*6*/ Task<string> contentsTask = httpClient.GetStringAsync("http://msdn.microsoft.com"); /*7*/ /*8*/ //wait and return... /*9*/ string contents = await contentsTask; /*10*/ /*11*/ //get the length... /*12*/

HttpContent.ReadAsStringAsync causes request to hang (or other strange behaviours)

偶尔善良 提交于 2019-12-10 03:22:22
问题 We are building a highly concurrent web application, and recently we have started using asynchronous programming extensively (using TPL and async / await ). We have a distributed environment, in which apps communicate with each other through REST APIs (built on top of ASP.NET Web API). In one specific app, we have a DelegatingHandler that after calling base.SendAsync (i.e., after calculating the response) logs the response to a file. We include the response's basic information in the log

Can/should Task<TResult> be wrapped in a C# 5.0 awaitable which is covariant in TResult?

℡╲_俬逩灬. 提交于 2019-12-09 15:03:02
问题 I'm really enjoying working with C# 5.0 asynchronous programming. However, there are a few places where updating old code to be consistent with the TAP model is causing problems for me. Here's one of them - I'm not sure exactly why Task<TResult> is not covariant in TResult, but it's causing problems for me when trying to update a covariant interface to move from a synchronous to an asychronous pattern: Old code: public interface IInitializable<out T> // ** out generic modifier ** { ///

C# 5.0 async await return a list

女生的网名这么多〃 提交于 2019-12-09 13:15:55
问题 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.cs public async Task Initialize(string objectPath) { AnObject someObject = await BClass.GetAnObject(objectPath); } and this is the called method Class B: public async Task<AnObject> GetAnObject(string objectPath) { AnObject someObj = new AnObject(); return someObj; } What happens if I want to