async-await

What's so special about UI thread?

余生颓废 提交于 2020-01-02 04:09:06
问题 Let's say I have a method fooCPU that runs synchronously (it doesn't call pure async methods performing I/O, or use other threads to run its code by calling Task.Run or similar ways). That method performs some heavy calculations - it's CPU bound. Now I call fooCPU in my program without delegating it to be executed by a worker thread. If one line of fooCPU will take long to run, no other lines will be executed until it finishes. So for example, calling it from the UI thread causes the UI

Asynchronous memory streaming approach: which of the following?

眉间皱痕 提交于 2020-01-02 03:46:06
问题 I am working on solution which uses asynchronous memory streaming and I am thinking about right approach for implementing such. Which one is more convenient? The first, simple: //First approach: linear async private async static Task WriteToStreamFirstVariant() { MemoryStream memoryStream = new MemoryStream(); byte[] data = new byte[256]; try { await memoryStream.WriteAsync(data, 0, data.Length); } catch(Exception exception) { //Handling exception } finally { memoryStream.Dispose(); } } Or

async/await vs. hand made continuations: is ExecuteSynchronously cleverly used?

廉价感情. 提交于 2020-01-02 03:34:12
问题 I recently wrote the following code: Task<T> ExecAsync<T>( string connectionString, SqlCommand cmd, Func<SqlCommand, T> resultBuilder, CancellationToken cancellationToken = default(CancellationToken) ) { var tcs = new TaskCompletionSource<T>(); SqlConnectionProvider p; try { p = GetProvider( connectionString ); Task<IDisposable> openTask = p.AcquireConnectionAsync( cmd, cancellationToken ); openTask .ContinueWith( open => { if( open.IsFaulted ) tcs.SetException( open.Exception.InnerExceptions

Concurrent execution of async methods

江枫思渺然 提交于 2020-01-02 03:29:09
问题 Using the async/await model, I have a method which makes 3 different calls to a web service and then returns the union of the results. var result1 = await myService.GetData(source1); var result2 = await myService.GetData(source2); var result3 = await myService.GetData(source3); allResults = Union(result1, result2, result3); Using typical await, these 3 calls will execute synchronously wrt each other. How would I go about letting them execute concurrently and join the results as they complete?

Async code without waiting for completion

烂漫一生 提交于 2020-01-02 03:27:07
问题 I'm currently having issues with IIS crashing out leaving the following messages in the event log. They're not too helpful in directing me to the actual source of the error but a bit of research suggests this is just a case of spawning tasks but not waiting for the result, when they do eventually complete if the parent process has completed it cannot be associated with the parent thread which causes a null reference exception. Is that correct? For the most part I have removed or added awaits

await/async Microsoft Practices Enterprise Library Data

 ̄綄美尐妖づ 提交于 2020-01-02 02:46:07
问题 I have an older application that I wrote where I used Microsoft.Practices.EnterpriseLibrary.Data to get data from the DB. I have recently upgraded to .NET 4.5 and wanted to advantage of await/async . I do not see any methods ending in "Async" as per the naming standard, even in the most recent version of the package. Is it possible to use await/async with this ADO .NET library without manually making it asynchronous? 回答1: I'm using an older version of the EL that offers Begin* / End* methods,

Returning Void in Async method from WEB API Controller

女生的网名这么多〃 提交于 2020-01-02 02:25:10
问题 I have this async method inside ASP.NET MVC 4 WEB API Controller that I got from this blog: http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/ public async Task<IList<RecivedFile>> Post() { List<RecivedFile> result = new List<RecivedFile>(); if (Request.Content.IsMimeMultipartContent()) { try { MultipartFormDataStreamProvider stream = new MultipartFormDataStreamProvider(HostingEnvironment.MapPath("~/USER-UPLOADS")); IEnumerable<HttpContent

How to consume HttpClient from F#?

匆匆过客 提交于 2020-01-02 01:05:10
问题 I'm new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#: var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); How to write the same in F#? 回答1: Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to

C# Polly async-await: Wait for user confirmation before retry

廉价感情. 提交于 2020-01-01 22:12:37
问题 I'm creating a Xamarin.Forms app for iOS and Android where I am keeping data and a local sqlite database and online in an Azure server. Although my app requires an internet connection which it is always checking with the Connectivity plugin, I've found that I will sometimes have an exception thrown if a user loses cell reception mid request. I want to have a method I can call all my server requests to that will retry the request if an error occurs. I would also like the ability to ask the

New HttpClient proxy settings issues

走远了吗. 提交于 2020-01-01 18:17:28
问题 I'm trying to rewrite old network authentication logic according to new fw 4.5 features such as HttpClient and await/async and i experience unexpected latency (around 15s) between request and response. My guess is that happens because client tries to find/use proxy from IE like it was with old HttpRequest/WebClient. Here's the code : public static async Task<AuthResult> GetDataFromServiceAsync(string url, string login, string password) { Debug.WriteLine("[" + DateTime.Now + "]