async-await

How can I run this entity framework sample faster

我只是一个虾纸丫 提交于 2019-12-18 18:25:35
问题 I have this code for adding about 1500 record to database: async void button7_Click(object sender, EventArgs e) { var task = await Task.Run(() => { Random rnd = new Random(); for (int i = 0; i <= 1500; i++) { db.Tbls.Add(new Tbl() { Name = "User" + i + 1, Num = rnd.Next(10, i + 10) / 10 }); progress.Report(i * 100 / 1500); } db.SaveChanges(); return db.Tbls.Count(); }); } But it took about 4 second to complete the process but because I used async/await it doesn't freezes the UI. Now my

how to use async and await in timer

旧街凉风 提交于 2019-12-18 17:04:35
问题 My windows app's requirement are: Using HttpWebRequest get web request/response every 3 seconds in one thread.(total is about 10 threads for doing this web request/response.) Each thread use some global variables. I want to use a System.Timers.Timer and async and await. But I don't know that is a best way for high performance. And then how to test them. I am a green in C#. 回答1: You could write a RepeatActionEvery() method as follows. It's parameters are: action - The action you want to repeat

What's wrong with this async Task method?

て烟熏妆下的殇ゞ 提交于 2019-12-18 17:00:19
问题 This is just a simple async task but I have always got strange compiler errors. This code from a Web API service in a ASP.NET 4 project, created with VS2010. Even ContinueWith (non-generic) returns Task implicitly but this error still exists. Code: public class TestController : ApiController { public Task<HttpResponseMessage> Test() { string url = "http://www.stackoverflow.com"; var client = new HttpClient(); return client.GetAsync(url).ContinueWith<HttpResponseMessage>((request) => { //

Has this usage of async / await in C# been discovered before? [closed]

…衆ロ難τιáo~ 提交于 2019-12-18 16:58:47
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . After a previous question on stackoverflow regarding async / await it seemed to me that await was much more powerful and general than the marketing suggested. It seems to be a general method of building

Async/await for long-running API methods with progress/cancelation

♀尐吖头ヾ 提交于 2019-12-18 16:53:08
问题 Edit I suppose the proper way of forcing await to invoke the worker asynchronously is with a Task.Run, like this: await Task.Run(() => builder.Build(dlg.FileName, cts.Token, new Progress(ReportProgress))); Got some light from http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx. this should be easy but I'm new to async/await so bear with me. I am building a class library exposing an API with some long-running operations. In the past, I used a BackgroundWorker to deal with progress

Why doesn't calling Task<T>.Result deadlock?

旧街凉风 提交于 2019-12-18 16:39:30
问题 After reading this post a few months ago, I became paranoid of getting the Result of a Task<T> and incessantly wrapped all of my calls to it with a ConfigureAwait(false) or Task.Run . However, for some reason the following code completes successfully: public static void Main(string[] args) { var arrays = DownloadMany(); foreach (var array in arrays); } IEnumerable<byte[]> DownloadMany() { string[] links = { "http://google.com", "http://microsoft.com", "http://apple.com" }; using (var client =

Return Task or await and ConfigureAwait(false)

霸气de小男生 提交于 2019-12-18 14:54:34
问题 Suppose to have a service library with a method like this public async Task<Person> GetPersonAsync(Guid id) { return await GetFromDbAsync<Person>(id); } Following the best practices for the SynchronizationContext is better to use public async Task<Person> GetPersonAsync(Guid id) { return await GetFromDbAsync<Person>(id).ConfigureAwait(false); } But when you have only one operation (I think) is better to return the Task directly. See At the end of an async method, should I return or await?

How is an IAsyncCursor used for iteration with the mongodb c# driver?

╄→гoц情女王★ 提交于 2019-12-18 14:17:29
问题 I'm trying to get a list of all the databases in my server and ultimately print them out (i.e. use their names as string s). With the previous version of the c# driver I could call the Server.GetDatabases() , but that has been replaced with ListDatabasesAsync() . The return value is an IAsyncCursor<> and I'm not sure what to do with it. How does one iterate through the list of databases (or anything) with such a cursor? 回答1: Short answer: use the ForEachAsync extension method: var cursor =

How is an IAsyncCursor used for iteration with the mongodb c# driver?

僤鯓⒐⒋嵵緔 提交于 2019-12-18 14:17:08
问题 I'm trying to get a list of all the databases in my server and ultimately print them out (i.e. use their names as string s). With the previous version of the c# driver I could call the Server.GetDatabases() , but that has been replaced with ListDatabasesAsync() . The return value is an IAsyncCursor<> and I'm not sure what to do with it. How does one iterate through the list of databases (or anything) with such a cursor? 回答1: Short answer: use the ForEachAsync extension method: var cursor =

Intellij Idea warning - “Promise returned is ignored” with aysnc/await

一个人想着一个人 提交于 2019-12-18 13:54:12
问题 I'm using Express.js in my code with Node.js v7.3. In this I've created a User Router which forwards the requests to my User Controller . I'm using async/await inside the User Controller to do asynchronous calls. The problem is that IntelliJ gives me a warning saying that Promise returned from login() is ignored. The thing is I'm not even returning anything from the login() method. Here's the code - UserRouter.js router.post('/login', function (req, res, next) { userController.login(req, res)