Task.Continuewith (on current thread)
async-await, c#, task-parallel-library, winforms
Solution
Pass `TaskScheduler.FromCurrentSynchronizationContext()` to `ContinueWith()`...
.ContinueWith((task) => {
// hide loading mas
Core.HideLoadingMask();
if (frm != null) this.Panel.Controls.Add(frm);
}, TaskScheduler.FromCurrentSynchronizationContext());
Problem
I have a method with the following code: ``` object frm = null; // shows the overlay loading mask Core.ShowLoadingMask("Please wait..."); // start task Task.Factory.StartNew(() => { // go to server and get the data var employee = new Data.Entities.Employee(employeeId); // instantiate the class type (reflection) frm = Activator.CreateInstance(type, employee ); }).ContinueWith((task) => { // hide loading mas Core.HideLoadingMask(); if (frm != null) this.Panel.Controls.Add(frm); }); ``` So, how can I force that the code inside `ContinueWith()` force to use the Current Thread, or maybe I'm doing it wrong. The process I need is: - Show Loading Mask before getting data from server - Get data from server (could take 3 seconds) - After that then exit the task and Hide the loading mask. Any clue?