473,395 Members | 1,689 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,395 software developers and data experts.

how to invoke method in thread from different thread?

Okay, I have the main thread which does all the work. This main thread
spawns a worker thread that just periodically poles the environment looking
for a certain condition. This second thread is very small, and only is
responsible for raising a flag when the environment changes.

What I want is for the main thread to raise an event when the change has
been flagged. I other words, I want the main thread to raise the event, not
this small worker thread. How can the worker thread talk to the main thread
so the resulting event can actually be raised in the main thread, not the
worker thread?
Nov 17 '05 #1
6 31843
Peter Rilling <pe***@nospam.rilling.net> wrote:
Okay, I have the main thread which does all the work. This main thread
spawns a worker thread that just periodically poles the environment looking
for a certain condition. This second thread is very small, and only is
responsible for raising a flag when the environment changes.

What I want is for the main thread to raise an event when the change has
been flagged. I other words, I want the main thread to raise the event, not
this small worker thread. How can the worker thread talk to the main thread
so the resulting event can actually be raised in the main thread, not the
worker thread?


The main thread would need to be "watching" for work to be done -
either periodically looking at a list of some kind or even just waiting
for items from that list, and doing nothing else.

The latter is a producer/consumer type of setup - see
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml about half
way down.

The former is very similar, just without the waiting.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
public delegate void TestDelegate(string name);
namespace ConditionChecker
{
public class TestApp
{
static TestApp _instance;
public event EventHandler ConditionMet;

public static TestApp Instance
{
get
{
lock(typeof(TestApp))
{
if (_instance == null) _instance = new TestApp();
}
return _instance;
}
}
[STAThread]
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.Name = "TestApp";
ConditionChecker cond = new ConditionChecker();
CheckDelegate del = new CheckDelegate(cond.Check);
del.BeginInvoke(null, null);
while(true)
{
if (TestApp.Instance.ConditionMet != null)
lock(TestApp.Instance)
{
TestApp.Instance.ConditionMet(null ,EventArgs.Empty);
}
}
}
}

public class ConditionChecker
{
public void HandleCondition(object sender, EventArgs e)
{
lock(this)
{
System.Console.WriteLine(string.Format("Condition Handled in
{0} @ {1}",
System.Threading.Thread.CurrentThread.Name,
DateTime.Now.TimeOfDay.ToString()));
TestApp.Instance.ConditionMet -= new
EventHandler(this.HandleCondition);
}
}
public void Check()
{
int i = 1;
System.Threading.Thread.CurrentThread.Name = "ConditionChecker";
while(true)
if (((i++) % 1000000) == 0)
{
System.Console.WriteLine("Condition Met in {0}",
System.Threading.Thread.CurrentThread.Name);
TestApp.Instance.ConditionMet += new
EventHandler(HandleCondition);
i = 1;
}

}
}

delegate void CheckDelegate();
}

Nov 17 '05 #3
namespace ConditionChecker
{
public class TestApp
{
static TestApp _instance;
public event EventHandler ConditionMet;

public static TestApp Instance
{
get
{
lock(typeof(TestApp))
{
if (_instance == null) _instance = new TestApp();
}
return _instance;
}
}
[STAThread]
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.Name = "TestApp";
ConditionChecker cond = new ConditionChecker();
CheckDelegate del = new CheckDelegate(cond.Check);
del.BeginInvoke(null, null);
while(true)
{
if (TestApp.Instance.ConditionMet != null)
lock(TestApp.Instance)
{
TestApp.Instance.ConditionMet(null ,EventArgs.Empty);
}
}
}
}

public class ConditionChecker
{
public void HandleCondition(object sender, EventArgs e)
{
lock(this)
{
System.Console.WriteLine(string.Format("Condition Handled in
{0} @ {1}",
System.Threading.Thread.CurrentThread.Name,
DateTime.Now.TimeOfDay.ToString()));
TestApp.Instance.ConditionMet -= new
EventHandler(this.HandleCondition);
}
}
public void Check()
{
int i = 1;
System.Threading.Thread.CurrentThread.Name = "ConditionChecker";
while(true)
if (((i++) % 1000000) == 0)
{
System.Console.WriteLine("Condition Met in {0}",
System.Threading.Thread.CurrentThread.Name);
TestApp.Instance.ConditionMet += new
EventHandler(HandleCondition);
i = 1;
}

}
}

delegate void CheckDelegate();
}
Nov 17 '05 #4
dont know how this line got in there:
public delegate void TestDelegate(string name);
here is the code:

=======================================
namespace ConditionChecker
{
public class TestApp
{
static TestApp _instance;
public event EventHandler ConditionMet;

public static TestApp Instance
{
get
{
lock(typeof(TestApp))
{
if (_instance == null) _instance = new TestApp();
}
return _instance;
}
}
[STAThread]
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.Name = "TestApp";
ConditionChecker cond = new ConditionChecker();
CheckDelegate del = new CheckDelegate(cond.Check);
del.BeginInvoke(null, null);
while(true)
{
if (TestApp.Instance.ConditionMet != null)
lock(TestApp.Instance)
{
TestApp.Instance.ConditionMet(null ,EventArgs.Empty);
}
}
}
}

public class ConditionChecker
{
public void HandleCondition(object sender, EventArgs e)
{
lock(this)
{
System.Console.WriteLine(
string.Format("Condition Handled in {0} @ {1}",
System.Threading.Thread.CurrentThread.Name,
DateTime.Now.TimeOfDay.ToString()));
TestApp.Instance.ConditionMet -=
new EventHandler(this.HandleCondition);
}
}
public void Check()
{
int i = 1;
System.Threading.Thread.CurrentThread.Name = "ConditionChecker";
while(true)
if (((i++) % 1000000) == 0)
{
System.Console.WriteLine("Condition Met in {0}",
System.Threading.Thread.CurrentThread.Name);
TestApp.Instance.ConditionMet +=
new EventHandler(HandleCondition);
i = 1;
}

}
}

delegate void CheckDelegate();
}
Nov 17 '05 #5
Peter,

Are you working in a windows Form? If so, then I'd recommend that the
simplest solution is to use "Invoke" to directly call the parent thread from
the worker thread.

http://www.yoda.arachsys.com/csharp/...winforms.shtml

There's a bit of typing in setting up the delegate, but I still think it's
the shortest and simplest solution.

The invoked method in the parent thread would look like this...

void OnCondition()
{
myEvent(); // Raise the event
}

and the call in the worker thread would look like...

if (conditionDetected)
{
Invoke(OnConditionDelegate);
}

Note that you can invoke a method with, or without, arguments.

HTH,

Javaman

"Peter Rilling" wrote:
Okay, I have the main thread which does all the work. This main thread
spawns a worker thread that just periodically poles the environment looking
for a certain condition. This second thread is very small, and only is
responsible for raising a flag when the environment changes.

What I want is for the main thread to raise an event when the change has
been flagged. I other words, I want the main thread to raise the event, not
this small worker thread. How can the worker thread talk to the main thread
so the resulting event can actually be raised in the main thread, not the
worker thread?

Nov 17 '05 #6
Check this one
http://winfx.msdn.microsoft.com/libr...ibrary/en-us/d
v_csref/html/3c7b02f9-4b0e-4b19-a6da-e2585f4fce85.asp

Nov 17 '05 #7

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

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
1
by: Phil | last post by:
How do I invoke a Word method on Words' thread, instead of on my app's thread? Background: My VB.NET app automates Word (2000 and onwards). After I do some editing (successfully) on a...
1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: Julia | last post by:
Hi, I have an application which run 3 different thread. the main application display a window like a console window in VS.NET. I would like that each thread will write to this window details...
1
by: Mike | last post by:
Hi! I need to call a method with some parameters on a form which was created on a different thread. Here's how I create the form: // Main Thread: ThreadStart entryPoint = new...
1
by: VMI | last post by:
Im using a separate thread in my code to copy a huge file, but I get the "Controls created on one thread cannot be parented to a control on a different thread" when I want to update the Form...
2
by: Henrik Dahl | last post by:
Hello! In my program I've made an STA thread which is running separately from the main thread. The STA thread is instantiating some COM objects. Is it possible, from the main thread, to invoke...
3
by: Michael Maes | last post by:
Hello, I know forms aren't thread-safe, but I'm a bit stuck here.... I have an MDI-Application which after 'Login' loads the Data from an SQL Server into DataSets. Those DataSets are Parented...
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...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.