How to execute method in main thread, when I am in secondary thread, but in Console application? I tried something like this, but context is null because I am using console application.
static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(Worker));
t.Start();
t.Join();
}
static void Worker()
{
// Create a SynchronizationContext object for the current thread
SynchronizationContext context = SynchronizationContext.Current;
// Call the HelloWorker method on the main thread using the SynchronizationContext
context.Post(new SendOrPostCallback(HelloWorker), null);
}
static void HelloWorker(object state)
{
// Code in this method will be executed on the main thread
Console.WriteLine("Hello from the main thread! Thread ID: " + Thread.CurrentThread.ManagedThreadId);
}