async-await

How Do I Call an Async Method from a Non-Async Method? [duplicate]

☆樱花仙子☆ 提交于 2019-12-17 18:34:49
问题 This question already has answers here : How to call asynchronous method from synchronous method in C#? (14 answers) Closed 2 years ago . I have the below method: public string RetrieveHolidayDatesFromSource() { var result = this.RetrieveHolidayDatesFromSourceAsync(); /** Do stuff **/ var returnedResult = this.TransformResults(result.Result); /** Where result gets used **/ return returnedResult; } private async Task<string> RetrieveHolidayDatesFromSourceAsync() { using (var httpClient = new

How do I reset a CancellationToken properly?

浪尽此生 提交于 2019-12-17 18:25:22
问题 I have been playing round with the async ctp this morning and have a simple program with a button and a label . Click the button and it starts updating the label , stop the button it stops writing to the label . However, I'm not sure how to reset the CancellationTokenSource so that I can restart the process. My code is below: public partial class MainWindow : Window { CancellationTokenSource cts = new CancellationTokenSource(); public MainWindow() { InitializeComponent(); button.Content =

How to avoid reentrancy with async void event handlers?

放肆的年华 提交于 2019-12-17 18:24:55
问题 In a WPF application, I have a class that receives messages over the network. Whenever an object of said class has received a full message, an event is raised. In the MainWindow of the application I have an event handler subscribed to that event. The event handler is guaranteed to be called on the GUI thread of the application. Whenever the event handler is called, the contents of the message needs to be applied to the model. Doing so can be quite costly (>200ms on current hardware). That's

RunAsync - How do I await the completion of work on the UI thread?

≯℡__Kan透↙ 提交于 2019-12-17 18:19:54
问题 When awaiting Dispatcher.RunAsync the continuation occurs when the work is scheduled, not when the work has completed. How can I await the work completing? Edit My original question assumed the premature continuation was caused by the design of the API, so here's the real question. When awaiting Dispatcher.RunAsync using an asynchronous delegate, using await within the delegate's code, the continuation occurs when the await is encountered, not when the work has completed. How can I await the

Async CTP - How can I use async/await to call a wcf service?

天大地大妈咪最大 提交于 2019-12-17 18:09:01
问题 If I call a WCF service method I would do something like this: proxy.DoSomethingAsync(); proxy.DoSomethingAsyncCompleted += OnDoSomethingAsyncCompleted; How could I do the same using the new async ctp? I guess I would need something like proxy.DoSomethingTaskAsync or proxy.DoSomethingAsync().ToTask() ? The web service call needs to return a Task<T> to be able to use the await keyword, but how?? 回答1: In the CTP there are factory methods that do the work of turning regular APM functions (Begin

Timeout an async method implemented with TaskCompletionSource

*爱你&永不变心* 提交于 2019-12-17 17:57:07
问题 I have a blackbox object that exposes a method to kick of an async operation, and an event fires when the operation is complete. I have wrapped that into an Task<OpResult> BlackBoxOperationAysnc() method using TaskCompletionSource - that works well. However, in that async wrapper I'd like to manage completing the async call with a timeout error if the event is not received after a given timeout. Currently I manage it with a timer as: public Task<OpResult> BlackBoxOperationAysnc() { var tcs =

async-await threading internals

非 Y 不嫁゛ 提交于 2019-12-17 17:53:29
问题 I'm curious about async await threading internals. Everyone states that async is so much better in case of performance, because it frees threads that are waiting for a response to a long asynchronous call. Ok I get it. But let's consider this scenario. I have an async methodA executing an async operation on database. The api of the database exposes function BeginQuery and event QueryCompleted. I wrapped those with a task (with use of TaskCompletionSource). My question is what is going under

The async and await keywords don't cause additional threads to be created?

牧云@^-^@ 提交于 2019-12-17 17:44:11
问题 I'm confused. How can one or many Task run in parallel on a single thread? My understanding of parallelism is obviously wrong. Bits of MSDN I can't wrap my head around: The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. .. and: Between starting a task and

Angular 4 async with loading and display when empty

折月煮酒 提交于 2019-12-17 17:42:04
问题 I have an Angular component that gets a service CatalogService injected: export class CatalogListComponent implements OnInit { catalog$: Observable<MovieResponseItem[]>; constructor(private catalogService: CatalogService) {} ngOnInit() { this.catalog$ = this.catalogService.userCatalog; } } This service returns an Observable<MovieResponseItem[]> on property userCatalog : @Injectable() export class CatalogService { get userCatalog(): Observable<MovieResponseItem[]> { return this.

Angular 4 async with loading and display when empty

99封情书 提交于 2019-12-17 17:42:02
问题 I have an Angular component that gets a service CatalogService injected: export class CatalogListComponent implements OnInit { catalog$: Observable<MovieResponseItem[]>; constructor(private catalogService: CatalogService) {} ngOnInit() { this.catalog$ = this.catalogService.userCatalog; } } This service returns an Observable<MovieResponseItem[]> on property userCatalog : @Injectable() export class CatalogService { get userCatalog(): Observable<MovieResponseItem[]> { return this.