I have one main thread(console app) which starts multiple child threads which do some IO intensive work. I want to send some messages from child thread to the main thread(Notifications of success/failure).
I am doing this using custom events, but am facing issues.
Here is my example code:
namespace myname
{
public delegate void NotifyParent(string msg); /Creating delegate
public class Test
{
public static event NotifyParent Stop; //Creating the event
static void Main(string[] args)
{
Stop += new NotifyParent(GetStatus); //Binding event to event handler
Thread t1 = new Thread(new ThreadStart(TestThread.DoWork));
Thread t2 = new Thread(new ThreadStart(TestThread.DoWork));
t1.start();
t2.start();
}
public static void DoWork(string msg)
{
Console.WriteLine(msg);
}
}//end of class Test
class TestThread
{
public event NotifyParent Stop;
public void DoWork()
{
//Do work
Stop("Message to main thread");
}
}
The place where is raise the event, that is in the TestThread class, my event Stop is always null. Hence I am not able to raise the event. I referred to may examples related to events and delegates. they do the same thing. I also tried exact code given in
these examples but same result.
Hi, No need to create event at the Test class, just do it like
namespace myname
{
public delegate void NotifyParent(string msg); //Creating delegate
public class Test
{
static void Main(string[] args)
{
TestThread tt = new TestThread();
tt.Stop += new NotifyParent(DoWork);
Thread t1 = new Thread(new ThreadStart(tt.DoWork));
Thread t2 = new Thread(new ThreadStart(tt.DoWork));
t1.Start();
t2.Start();
}
public static void DoWork(string msg)
{
Console.WriteLine(msg);
}
}//end of class Test
class TestThread
{
public event NotifyParent Stop;
public void DoWork()
{
//Do work
Stop("Message to main thread");
}
}
}
bhavay11
Member
66 Points
31 Posts
Sending message from one thread to another
Mar 07, 2012 05:35 AM|LINK
Hi,
I have one main thread(console app) which starts multiple child threads which do some IO intensive work. I want to send some messages from child thread to the main thread(Notifications of success/failure).
I am doing this using custom events, but am facing issues.
Here is my example code:
namespace myname { public delegate void NotifyParent(string msg); /Creating delegate public class Test { public static event NotifyParent Stop; //Creating the event static void Main(string[] args) { Stop += new NotifyParent(GetStatus); //Binding event to event handlerThread t1 = new Thread(new ThreadStart(TestThread.DoWork)); Thread t2 = new Thread(new ThreadStart(TestThread.DoWork)); t1.start(); t2.start(); } public static void DoWork(string msg) { Console.WriteLine(msg); } }//end of class Test class TestThread { public event NotifyParent Stop; public void DoWork() { //Do work Stop("Message to main thread"); } }The place where is raise the event, that is in the TestThread class, my event Stop is always null. Hence I am not able to raise the event. I referred to may examples related to events and delegates. they do the same thing. I also tried exact code given in these examples but same result.
Thank you
Mastan Oli
Contributor
5088 Points
998 Posts
Re: Sending message from one thread to another
Mar 07, 2012 05:56 AM|LINK
Hi, No need to create event at the Test class, just do it like
namespace myname { public delegate void NotifyParent(string msg); //Creating delegate public class Test { static void Main(string[] args) { TestThread tt = new TestThread(); tt.Stop += new NotifyParent(DoWork); Thread t1 = new Thread(new ThreadStart(tt.DoWork)); Thread t2 = new Thread(new ThreadStart(tt.DoWork)); t1.Start(); t2.Start(); } public static void DoWork(string msg) { Console.WriteLine(msg); } }//end of class Test class TestThread { public event NotifyParent Stop; public void DoWork() { //Do work Stop("Message to main thread"); } } }playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful