async-await

Updating UI from events using asyc await

别说谁变了你拦得住时间么 提交于 2019-12-18 12:49:37
问题 I am trying to understand how to update a UI from an event while using async/await pattern. Below is the test code I am using on a WinForm app. I am not even sure this is the right way to go about it. What is necessary to allow the pwe_StatusUpdate method to update the UI? The cross-thread operation error is thrown there. Thanks for reading. // calling code ProcessWithEvents pwe = new ProcessWithEvents(); pwe.StatusUpdate += pwe_StatusUpdate; await pwe.Run(); void pwe_StatusUpdate(string

Why no-return-await vs const x = await?

喜欢而已 提交于 2019-12-18 12:49:12
问题 What is the difference between return await foo() and const t = await foo(); return t http://eslint.org/docs/rules/no-return-await 回答1: Basically, because return await is redundant. Look at it from a slightly higher level of how you actually use an async function: const myFunc = async () => { return await doSomething(); }; await myFunc(); Any async function is already going to return a Promise , and must be dealt with as a Promise (either directly as a Promise , or by also await -ing. If you

Should we use CancellationToken with MVC/Web API controllers?

≡放荡痞女 提交于 2019-12-18 12:44:52
问题 There are different examples for async controllers. Some of them use CancellationToken in method definition: public async Task<ActionResult> ShowItem(int id, CancellationToken cancellationToken) { await Database.GetItem(id, cancellationToken); ... But other examples and even the default ASP.NET projects for VS2013 don't use CancellationToken at all and work without it: public async Task<ActionResult> ShowItem(int id) { await Database.GetItem(id); ... It's not clear, if we should use

How to use async/await to achieve asynchronous page in asp.net webform?

久未见 提交于 2019-12-18 12:25:12
问题 We can now use the async/await key words in ASP.NET MVC 4. public async Task<ActionResult> TestAsync() { WebClient client = new WebClient(); return Content(await client.DownloadStringTaskAsync("http://www.google.com")); } But how to use it in ASP.NET WebForms? 回答1: One easy way is to just make your event handlers async . First, add the Async="true" parameter to the @Page directive, and then you should be able to write async event handlers as such: protected async void Page_Load(object sender,

Async ShowDialog

柔情痞子 提交于 2019-12-18 12:23:17
问题 I'm using async/await to asynchronously load my data from database and during the loading process, I want to popup a loading form, it's just a simple form with running progress bar to indicate that there's a running process. After data has been loaded, the dialog will automatically be closed. How can I achieve that ? Below is my current code: protected async void LoadData() { ProgressForm _progress = new ProgressForm(); _progress.ShowDialog() // not working var data = await GetData();

Async ShowDialog

吃可爱长大的小学妹 提交于 2019-12-18 12:23:06
问题 I'm using async/await to asynchronously load my data from database and during the loading process, I want to popup a loading form, it's just a simple form with running progress bar to indicate that there's a running process. After data has been loaded, the dialog will automatically be closed. How can I achieve that ? Below is my current code: protected async void LoadData() { ProgressForm _progress = new ProgressForm(); _progress.ShowDialog() // not working var data = await GetData();

Enforce an async method to be called once

六月ゝ 毕业季﹏ 提交于 2019-12-18 12:21:55
问题 Say I have a class that needs to perform some async initialization using an InitializeAsync() method. I want to make sure that the initialization is performed only once. If another thread calls this method while the initalization is already in progress, it will "await" until the first call returns. I was thinking about the following imlementation (using SemaphoreSlim). Is there a better/simpler approach? public class MyService : IMyService { private readonly SemaphoreSlim mSemaphore = new

Node wait for async function before continue

試著忘記壹切 提交于 2019-12-18 12:19:01
问题 I have a node application that use some async functions. How can i do for waiting the asynchronous function to complete before proceeding with the rest of the application flow? Below there is a simple example. var a = 0; var b = 1; a = a + b; // this async function requires at least 30 sec myAsyncFunction({}, function(data, err) { a = 5; }); // TODO wait for async function console.log(a); // it must be 5 and not 1 return a; In the example, the element " a " to return must be 5 and not 1. It

Using promises to await triggered events

冷暖自知 提交于 2019-12-18 12:16:27
问题 In the following code, thing is an external object that I don't control; I can't change how the event system of thing works. When fn is called, we return a promise whose executor listens to an event and then begins awaiting a series of functions which eventually result in that event being triggered: function fn() { return new Promise(async function(resolve, reject) { // This handler must be attached before `c` is called thing.once('myEvent', function(e) { resolve(e.data); // done }); // The

Nunit async test exception assertion

空扰寡人 提交于 2019-12-18 12:07:12
问题 I have a controller UserController with this action // GET /blah public Task<User> Get(string domainUserName) { if (string.IsNullOrEmpty(domainUserName)) { throw new ArgumentException("No username specified."); } return Task.Factory.StartNew( () => { var user = userRepository.GetByUserName(domainUserName); if (user != null) { return user; } throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, string.Format("{0} - username does not exist", domainUserName))); });