D
DaTurk
In the code below how would I get the callbackFromWorker() method to
execute on the thread named "MainThread". Right now when this program
runs it always reports that the callbackFromWorker() method is
running
on the thread named "WorkerThread".
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static System.Threading.ThreadStart workerMethod;
static System.Threading.ThreadStart mainMethod;
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.Name =
"MainThread";
workerMethod = new ThreadStart(doWork);
mainMethod = new ThreadStart(callbackFromWorker);
System.Threading.Thread workerThread = new
Thread(workerMethod);
workerThread.Name = "WorkerThread";
workerThread.Start();
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
static void callbackFromWorker()
{
Console.WriteLine("callbackFromWorker -> Running on:
{0}",
System.Threading.Thread.CurrentThread.Name);
}
static void doWork()
{
while (true)
{
Console.WriteLine("doWork -> Running on: {0}",
System.Threading.Thread.CurrentThread.Name);
Thread.Sleep(3000);
mainMethod.Invoke();
}
}
}
- Hide quoted text -
- Show quoted text -
execute on the thread named "MainThread". Right now when this program
runs it always reports that the callbackFromWorker() method is
running
on the thread named "WorkerThread".
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static System.Threading.ThreadStart workerMethod;
static System.Threading.ThreadStart mainMethod;
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.Name =
"MainThread";
workerMethod = new ThreadStart(doWork);
mainMethod = new ThreadStart(callbackFromWorker);
System.Threading.Thread workerThread = new
Thread(workerMethod);
workerThread.Name = "WorkerThread";
workerThread.Start();
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
static void callbackFromWorker()
{
Console.WriteLine("callbackFromWorker -> Running on:
{0}",
System.Threading.Thread.CurrentThread.Name);
}
static void doWork()
{
while (true)
{
Console.WriteLine("doWork -> Running on: {0}",
System.Threading.Thread.CurrentThread.Name);
Thread.Sleep(3000);
mainMethod.Invoke();
}
}
}
- Hide quoted text -
- Show quoted text -