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

First, let’s clear up some terminology: “asynchronous” (async) means that it may yield control back to the calling thread before it starts. In an async method, those “yield” points are await expressions. This is very different than the term “asynchronous”, as (mis)used by the MSDN documentation for years to mean “executes on a background thread”. … Read more

How to cancel a Task in await?

Read up on Cancellation (which was introduced in .NET 4.0 and is largely unchanged since then) and the Task-Based Asynchronous Pattern, which provides guidelines on how to use CancellationToken with async methods. To summarize, you pass a CancellationToken into each method that supports cancellation, and that method must check it periodically. private async Task TryTask() … Read more

C# HttpClient 4.5 multipart/form-data upload

my result looks like this: public static async Task<string> Upload(byte[] image) { using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent(“Upload—-” + DateTime.Now.ToString(CultureInfo.InvariantCulture))) { content.Add(new StreamContent(new MemoryStream(image)), “bilddatei”, “upload.jpg”); using ( var message = await client.PostAsync(“http://www.directupload.net/index.php?mode=upload”, content)) { var input = await message.Content.ReadAsStringAsync(); return !string.IsNullOrWhiteSpace(input) ? Regex.Match(input, @”http://\w*\.directupload\.net/images/\d*/\w*\.[a-z]{3}”).Value : null; } … Read more