Forcing a Method to be executed on the Main Thread

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 -
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

DaTurk said:
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".

I do not know of any method where you can force a method to be executed in a
given thread UNLESS, that thread has a message pump (like in the case of the
win apps).
If you think about it , a thread is executing something, how can you force
it to stop the flow of the method it's executing and then execute other
method?

As I said, only if the target thread has a message pump like feature will
your escenario work.
 
B

Brian Gideon

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();
}
}
}

You'll need to setup your main thread with a messaging pumping
mechanism to accomplish that. A blocking queue in conjunction with
the ISynchronizeInvoke makes for a nice pattern. And it's really not
all that difficult as long as you get a hold of solid thread-safe
implementation of a blocking queue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top