473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Forcing a Method to be executed on the Main Thread

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 -

Aug 30 '07 #1
2 4287
Hi,

"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
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.
Aug 30 '07 #2
On Aug 30, 1:25 pm, DaTurk <mmagd...@hotmail.comwrote:
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.

Aug 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ramesh | last post by:
hi, I have created one Component using C#. it have once class name is BusinessObject. In this class i have one method "CreatedRecords". When i make the build it created ComponentName.dll file in...
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
5
by: Hao L | last post by:
For example, void WorkerMethod() { ... UnregisterAllHotkeys();} void UnregisterAllHotKeys { for(...) {UnregisterHotKey(...);}} UnregisterHotKey is an API function that must be on the thread...
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
11
by: S Shulman | last post by:
Hi I am looking for a method that allows calling a method that will be executed by the main application thread bu will be called from any thread that I create manually. (I think that...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
6
by: DaTurk | last post by:
I know in a WinForm you can check if an Invoke is inquired and invoke a method on the main thread. But can you do this in a class? Can I call Invoke(someMethod) in my asynch event handler? And...
16
by: Paul Schwann | last post by:
Hi group, I am relatively new to C# (although I have a lot of programming excperience in other languages like Java and C). Currently I am searching for a solution to this problem: Suppose you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.