async-await

TAP global exception handler

久未见 提交于 2019-12-27 09:48:22
问题 This code throws an exception. Is it possible to define an application global handler that will catch it? string x = await DoSomethingAsync(); Using .net 4.5 / WPF 回答1: This is actually a good question, if I understood it correctly. I initially voted to close it, but now retracted my vote. It is important to understand how an exception thrown inside an async Task method gets propagated outside it. The most important thing is that such exception needs to be observed by the code which handles

A second operation started on this context before a previous asynchronous operation completed with UnitofWork and async

谁说我不能喝 提交于 2019-12-25 19:36:29
问题 A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe. My unitofwork code public class UnitOfWork : IUnitOfWork { private readonly CAMSDbEntities _context; private bool _disposed; public Dictionary<Type, object> repositories = new Dictionary<Type, object>(); private Guid _objectId

“finally” not executed because of Async in “try”?

北城以北 提交于 2019-12-25 19:04:03
问题 I have a bool ( b ) which appears only twice in my code (besides its declaration): try { b = true; //code including await SomeAsync(); } catch { } finally { b = false; } yet sometimes the try block is started with b being true (- before b = true ) which should never be happening because the declaration leaves it false , as does the previous finally . At some point, this code is executed in a loop, which in some cases is iterating quickly and SomeAsync() is trying to use too much memory. I

Async/ Await: why does the code that follows await is also executed on the background thread and not on the original primary thread?

北慕城南 提交于 2019-12-25 17:46:21
问题 Below is my code: class Program { static async Task Main(string[] args) { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); string message = await DoWorkAsync(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId); Console.WriteLine(message); } static async Task<string> DoWorkAsync() { return await Task.Run(() => { Thread.Sleep(3_000); return "Done with work!"; }); } } and the output is 1 // after 3 secs 3 Done with work! so you can see the main thread(id is 1) changed to worker

Async Await does not work as expected [duplicate]

你。 提交于 2019-12-25 12:38:09
问题 This question already has answers here : Using async/await with a forEach loop (18 answers) Closed 2 years ago . Currently we are storing short strings as keys. These keys correspond to long values which are labels. I am trying to update the corresponding long value for the key. But the console.log(record) always executes first and then the inner log statement executes which is not is desired. It always sends the unmodified record to the getRadioValues function caller. I want to return the

Async Await Handler Deadlock

断了今生、忘了曾经 提交于 2019-12-25 09:43:06
问题 I'm stuck in an Async deadlock and I can't figure out the correct syntax to fix it. I've looked at several different solutions, but can't seem to quite figure out what is causing the problem. I am using Parse as a backend and trying to use a handler to write to the table. My handler looks something like: public class VisitorSignupHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { //Get the user's name and email address var UserFullName = context.Request.QueryString[

Exception is not caught - AggregateException unhandled

房东的猫 提交于 2019-12-25 09:19:08
问题 Do anyone know why the following code does not catch my ConnectionException, I've spend hours with it... public async Task LoadContacts(string filter) { // We will send find contacts message to all servers supporting addressbook. var addressBookServers = _addressBookService.GetAddressBookServerList(); // Create task array here to be able to run each task in parallel manner. var tasksToProcess = new List<Task<SearchContactsResultDto>>(addressBookServers.Count); for (int i = 0; i <

Using WinRT on the Desktop with LockScreen API

让人想犯罪 __ 提交于 2019-12-25 07:47:09
问题 I'm writing a toy desktop app that interacts the LockScreen API, which is part of WinRT and uses async/await . After following the instructions here, I've enabled support for WinRT and added the following references: System.Runtime.dll System.Runtime.WindowsRuntime.dll System.Runtime.InteropServices.WindowsRuntime.dll However I still get this error while building: 'System.Runtime.CompilerServices.TaskAwaiter' does not contain a definition for 'IsCompleted' The error is associated with the

Nested Async Download - Async within Async

浪尽此生 提交于 2019-12-25 07:26:35
问题 I have some nested async methods calling each other and it is confusing. I am trying to convert a project which downloads the files in an async download. On the click of the download button this is the method triggered: private async void enableOfflineModeToolStripMenuItem_Click(object sender, EventArgs e) { for(int i = 0; i < _playlists.Count; i++) { DoubleDimList.Add(new List<String>()); for(int j = 0; j < 5; j++) { string sMp3 = IniReadValue(_playlists[i], "Track " + j); DoubleDimList[i]

How to cancel async work gracefully?

喜你入骨 提交于 2019-12-25 04:27:23
问题 I have a UI application which logs something every second. Every time OpenFileHandler is fired, I must start logging to a new file. This is a very simplified version of the code: string _logItem; string _fileName; CancellationTokenSource _cts; Task _task; private void OpenFileHandler(object sender, EventArgs e) { // once OpenFileHandler has been fired, // the _logItem should go to the new file if (_task != null && !_task.IsCompleted) { _cts.Cancel(); // can't do: _task.Wait(); } if (