c#-5.0

Captured Closure (Loop Variable) in C# 5.0

社会主义新天地 提交于 2019-11-26 12:24:58
This works fine (means as expected) in C# 5.0: var actions = new List<Action>(); foreach (var i in Enumerable.Range(0, 10)) { actions.Add(() => Console.WriteLine(i)); } foreach (var act in actions) act(); Prints 0 to 9. But this one shows 10 for 10 times: var actions = new List<Action>(); for (var i = 0; i < 10; i++) { actions.Add(() => Console.WriteLine(i)); } foreach (var act in actions) act(); Question: This was a problem that we had in C# versions before 5.0; so we had to use a loop-local placeholder for the closure and it's fixed now - in C# 5.0 - in "foreach" loops. But not in "for"

Request.Content.ReadAsMultipartAsync never returns

前提是你 提交于 2019-11-26 12:03:42
问题 I have an API for a system written using the ASP.NET Web Api and am trying to extend it to allow images to be uploaded. I have done some googling and found how the recommended way to accept files using MultpartMemoryStreamProvider and some async methods but my await on the ReadAsMultipartAsync never returns. Here is the code: [HttpPost] public async Task<HttpResponseMessage> LowResImage(int id) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode

How do you create an asynchronous method in C#?

℡╲_俬逩灬. 提交于 2019-11-26 11:45:19
问题 Every blog post I\'ve read tells you how to consume an asynchronous method in C#, but for some odd reason never explain how to build your own asynchronous methods to consume. So I have this code right now that consumes my method: private async void button1_Click(object sender, EventArgs e) { var now = await CountToAsync(1000); label1.Text = now.ToString(); } And I wrote this method that is CountToAsync : private Task<DateTime> CountToAsync(int num = 1000) { return Task.Factory.StartNew(() =>

Using async without await

痴心易碎 提交于 2019-11-26 11:20:29
问题 I\'d like to make a function async, so I simply add async like this: public async static void something(){ } You can see that its return-type is void . I just want this function to be called asynchronously without blocking, since return is void so no await is needed. But Visual Studio 2012 just cannot compile this, it says that I miss await ? Could you please advise a sample that makes a function async without using await . 回答1: I think that maybe you misunderstand what async does. The

Is there any async equivalent of Process.Start?

旧时模样 提交于 2019-11-26 11:09:41
Like the title suggests, is there an equivalent to Process.Start (allows you run another application or batch file) that I can await? I'm playing with a small console app and this seemed like the perfect place to be using async and await but I can't find any documentation for this scenario. What I'm thinking is something along these lines: void async RunCommand() { var result = await Process.RunAsync("command to run"); } Process.Start() only starts the process, it doesn't wait until it finishes, so it doesn't make much sense to make it async . If you still want to do it, you can do something

Difference between the TPL & async/await (Thread handling)

家住魔仙堡 提交于 2019-11-26 10:08:49
问题 Trying to understanding the difference between the TPL & async / await when it comes to thread creation. I believe the TPL ( TaskFactory.StartNew ) works similar to ThreadPool.QueueUserWorkItem in that it queues up work on a thread in the thread pool. That\'s of course unless you use TaskCreationOptions.LongRunning which creates a new thread. I thought async / await would work similarly so essentially: TPL: Factory.StartNew( () => DoSomeAsyncWork() ) .ContinueWith( (antecedent) => {

How does Task<int> become an int?

空扰寡人 提交于 2019-11-26 10:07:54
问题 We have this method: async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync(\"http://msdn.microsoft.com\"); // You can do work here that doesn\'t rely on the string from GetStringAsync. DoIndependentWork(); string urlContents = await getStringTask; //The thing is that this returns an int to a method that has a return type of Task<int> return urlContents.Length; } Does an implicit conversion occur between Task<int> and int

Async exception handling with void

六月ゝ 毕业季﹏ 提交于 2019-11-26 09:56:54
问题 I\'m using Async CTP to write an IO heavy console app. But I\'m having problems with exceptions. public static void Main() { while (true) { try{ myobj.DoSomething(null); } catch(Exception){} Console.Write(\"done\"); //... } } //... public async void DoSomething(string p) { if (p==null) throw new InvalidOperationException(); else await SomeAsyncMethod(); } And the following happens: \"done\" gets written to the console, then I get the exception in the debugger, then I press continue my program

How does await async work in C# [closed]

馋奶兔 提交于 2019-11-26 09:20:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 9 months ago . I am trying to understand how await async work in C# and one thing is confusing me a lot. I understand that any method that uses await keyword must be marked with async. My understanding is that when a line with await keyword is hit the code below that line is not executed. An

Is async/await suitable for methods that are both IO and CPU bound?

冷暖自知 提交于 2019-11-26 09:17:08
问题 The MSDN documentation appears to state that async and await are suitable for IO-bound tasks whereas Task.Run should be used for CPU-bound tasks. I\'m working on an application that performs HTTP requests to retrieve HTML documents, which it then parses. I have a method that looks like this: public async Task<HtmlDocument> LoadPage(Uri address) { using (var httpResponse = await new HttpClient().GetAsync(address)) //IO-bound using (var responseContent = httpResponse.Content) using (var