I know there is a similar question: Continue with the task in the main thread
but this question is more related to wpf, and I don’t see it working on a console application.
I want to execute a method on another thread, and when this method is completed, I want to continue execution on the main thread. I do not want to join the method. anyway here is what i have:
class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.Name = "MAIN";
DoWork(x =>
{
Console.Write("Method successfully executed. Executing callback method in thread:" +
"\n" + Thread.CurrentThread.Name);
});
Console.Read();
}
static void DoWork(Action<bool> onCompleteCallback)
{
Console.Write(Thread.CurrentThread.Name);
Task doWork = new Task(() =>
{
Console.Write(Thread.CurrentThread.Name);
Thread.Sleep(4000);
});
Action<Task> onComplete = (task) =>
{
onCompleteCallback(true);
};
doWork.Start();
doWork.ContinueWith(onComplete, TaskScheduler.FromCurrentSynchronizationContext());
}
}
How can I execute the onCompleteCallback method in the main thread?
source
share