async-await

XmlWriter async methods

牧云@^-^@ 提交于 2019-12-23 07:16:40
问题 I have found example of async using of XmlWriter within msdn documentation http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx async Task TestWriter(Stream stream) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Async = true; using (XmlWriter writer = XmlWriter.Create(stream, settings)) { await writer.WriteStartElementAsync("pf", "root", "http://ns"); await writer.WriteStartElementAsync(null, "sub", null); await writer.WriteAttributeStringAsync(null, "att", null

Where to use concurrency when calling an API

淺唱寂寞╮ 提交于 2019-12-23 07:16:09
问题 Inside a c# project I'm making some calls to a web api, the thing is that I'm doing them within a loop in a method. Usually there are not so many but even though I was thinking of taking advantage of parallelism. What I am trying so far is public void DeployView(int itemId, string itemCode, int environmentTypeId) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ApiUrl"]); client.DefaultRequestHeaders.Accept.Clear(); client

Result vs raise in F# async?

情到浓时终转凉″ 提交于 2019-12-23 07:08:29
问题 It seems like there are two ways to return errors in an async workflow: raise and Result . let willFailRaise = async { return raise <| new Exception("oh no!") } let willFailResult = async { return Result.Error "oh no!" } For the caller, the handling is a bit different: async { try let! x = willFailRaise // ... with error -> System.Console.WriteLine(error) } async { let! maybeX = willFailResult match maybeX with | Result.Ok x -> // ... | Result.Error error -> System.Console.WriteLine(error) }

Wrap a synchronous function in Asynchronous call c#

自闭症网瘾萝莉.ら 提交于 2019-12-23 05:25:54
问题 I have a server, which must poll an equipment through the network, to obtain information, process it and then send it to the users. The equipment survey becomes a synchronous blocking function. My question is: How to create an own asynchronous function version to perform this function using Task or another asynchronous pattern??? Consider the following code to get information from equipment: IEnumerable<Logs> GetDataFromEquipment(string ipAddress) { Equipment equipment = new Equipment(); /

http client frozes in windows form app

拈花ヽ惹草 提交于 2019-12-23 05:22:16
问题 I am following this example, and it works in a console application, but then I tried in a windows form app and it forzes, when in hits the line await client.GetAsync("api/branches/1035") how is it diferent? console code (this works): static void Main() { RunAsync().Wait(); } static async Task RunAsync() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:49358/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new

Web service running unexpectedly synchronously

自古美人都是妖i 提交于 2019-12-23 05:05:42
问题 We've got a web service that needs to connect to a slow database process and returns a value to the client, so are trying to run it asynchronously to stop it blocking the thread. However, if we make two calls they run sequentially, not asynchronously at all. Call 1 returns after 5 seconds, call 2 after 10, call 3 after 15 and so on. Here's the (simplified test version) code - public class ImportAsyncController : AsyncController { [HttpPost] [Authorize(Roles = "Admin, ContentAdmin, ContentEdit

Async TCP Server in .net 4.5 using asyn and await, data loss is happening

谁说胖子不能爱 提交于 2019-12-23 05:00:56
问题 I have to implement asyn tcp serve in .net 4.5 using async & await for this by referring the Link I have implemented the server My server code is as below public class AsyncEchoServer { private int _listeningPort; public AsyncEchoServer( int port) { _listeningPort = port; } public async void Start() { IPAddress ipAddre = IPAddress.Parse("192.168.2.4"); TcpListener listener = new TcpListener(ipAddre, _listeningPort); listener.Start(); LogMessage("Server is running"); while (true) { try { var

Why the need of using await keyword for calling async method

一笑奈何 提交于 2019-12-23 04:58:08
问题 const tryToGetResult = async () => { const networkResult = await someNetworkRequest(); console.log(networkResult); //Make sure networkResult is resolved to an object return networkResult; } const result = tryToGetResult(); networkResult is already an object and the flow has been taken care of by using async/await . But I don't understand why const result = tryToGetResult() , the result variable here is getting a promise instead? I understand that the situation can be fixed by adding await as

Parallel request to scrape multiple pages of a website

孤人 提交于 2019-12-23 04:54:40
问题 I want to scrape a website with plenty of pages with interesting data but as the source is very large I want to multithread and limit the overload. I use a Parallel.ForEach to start each chunk of 10 tasks and I wait in the main for loop until the numbers of active threads started drop below a threshold. For that I use a counter of active threads I increment when starting a new thread with a WebClient and decrement when the DownloadStringCompleted event of the WebClient is triggered.

Waiting on tasks in ASP.NET WebPages Razor view

笑着哭i 提交于 2019-12-23 04:54:09
问题 I am using async/await, and calling an async method from one of my views and I need to wait until it finishes. I've seen a lot of examples for ASP.NET MVC, where you can just put an "async" into the signature of your action. But I haven't seen any examples for ASP.NET WebPages. Do I just call "Wait()" on the returned task inside my Razor view? I've seen recommendations against Wait(). Please give me some references/examples on how to properly call async methods from within a Razor view in