async-await

How to better handle disposed controls when using async/await

孤者浪人 提交于 2020-05-28 12:24:47
问题 Consider this code that runs on the UI thread: dividends = await Database.GetDividends(); if (IsDisposed) return; //Do expensive UI work here earnings = await Database.GetEarnings(); if (IsDisposed) return; //Do expensive UI work here //etc... Note that every time I await I also check IsDisposed . It's necessary because say I await on a long running Task . Meanwhile the user closes the form before it completes. The Task will finish and run a continuation that attempts to access controls on a

How to better handle disposed controls when using async/await

落爺英雄遲暮 提交于 2020-05-28 12:24:47
问题 Consider this code that runs on the UI thread: dividends = await Database.GetDividends(); if (IsDisposed) return; //Do expensive UI work here earnings = await Database.GetEarnings(); if (IsDisposed) return; //Do expensive UI work here //etc... Note that every time I await I also check IsDisposed . It's necessary because say I await on a long running Task . Meanwhile the user closes the form before it completes. The Task will finish and run a continuation that attempts to access controls on a

Mongoose pass data out of withTransaction helper

我的未来我决定 提交于 2020-05-27 13:11:45
问题 Introduction Hey there, I am trying to pass out data from the mongoose withTransaction callback. Right now, I am using the following code which implements callbacks: const transactionSession = await mongoose.startSession() await transactionSession.withTransaction(async (tSession) => { try { // MARK Transaction writes & reads removed for brevity console.log("Successfully performed transaction!") cb(null, "Any test data") return Promise.resolve() } catch (error) { console.log("Transaction

Mongoose pass data out of withTransaction helper

寵の児 提交于 2020-05-27 13:09:59
问题 Introduction Hey there, I am trying to pass out data from the mongoose withTransaction callback. Right now, I am using the following code which implements callbacks: const transactionSession = await mongoose.startSession() await transactionSession.withTransaction(async (tSession) => { try { // MARK Transaction writes & reads removed for brevity console.log("Successfully performed transaction!") cb(null, "Any test data") return Promise.resolve() } catch (error) { console.log("Transaction

How to call async method from MainWindow?

有些话、适合烂在心里 提交于 2020-05-27 13:08:18
问题 So I wrote a quick async method to get some data into a DataTable from an Oracle db. How am I suppose to call this from MainWindow() without blocking UI thread? The async / wait model doesn't really make much sense there. async Task<DataTable> AccessOracleAsync() { DataTable dt; using(OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString)) using (OracleCommand cmd = new OracleCommand(@"SELECT * FROM myTbl", conn)) { await conn

How to call async method from MainWindow?

血红的双手。 提交于 2020-05-27 13:07:40
问题 So I wrote a quick async method to get some data into a DataTable from an Oracle db. How am I suppose to call this from MainWindow() without blocking UI thread? The async / wait model doesn't really make much sense there. async Task<DataTable> AccessOracleAsync() { DataTable dt; using(OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString)) using (OracleCommand cmd = new OracleCommand(@"SELECT * FROM myTbl", conn)) { await conn

How to call async method from MainWindow?

会有一股神秘感。 提交于 2020-05-27 13:06:28
问题 So I wrote a quick async method to get some data into a DataTable from an Oracle db. How am I suppose to call this from MainWindow() without blocking UI thread? The async / wait model doesn't really make much sense there. async Task<DataTable> AccessOracleAsync() { DataTable dt; using(OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString)) using (OracleCommand cmd = new OracleCommand(@"SELECT * FROM myTbl", conn)) { await conn

How to update GUI continuously with async

霸气de小男生 提交于 2020-05-27 12:39:11
问题 Just created a WPF project .net 4.6 And have put this code inside lbl1 is a label on the GUI But the label is never updated or the while loop continue only 1 time private void Button_Click_1(object sender, RoutedEventArgs e) { var t = Task.Run( async () => { await AsyncLoop(); }); } async Task AsyncLoop() { while (true) { string result = await LoadNextItem(); lbl1.Content = result; } } private static int ir11 = 0; async Task<string> LoadNextItem() { ir11++; return "aa " + ir11; } 回答1: Please

Paradoxical issue with mocha done() and async await

别来无恙 提交于 2020-05-26 11:45:05
问题 I have the following test case: it("should pass the test", async function (done) { await asyncFunction(); true.should.eq(true); done(); }); Running it asserts: Error: Resolution method is overspecified. Specify a callback or return a Promise; not both. And if I remove the done(); statement, it asserts: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. How to solve this paradox? 回答1: You need to remove the done

Async lambda expression with await returns Task?

我与影子孤独终老i 提交于 2020-05-26 10:22:51
问题 I have the following code: // Get all of the files from the local storage directory. var files = await folder.GetFilesAsync(); // Map each file to a stream corresponding to that file. var streams = files.Select(async f => { return await f.OpenStreamForWriteAsync(); }); I would expect streams to be of type IEnumerable<Stream> but in fact it is of IEnumberable<Task<Stream>> , which is what I would've expected had I omitted the await keyword. The return type of OpenStreamForWriteAsync is Task