I’m trying to make my first application where I use event to communicate between two threads. I don’t understand working with delegates very well so I need advice how to finish my application and what are my mistakes (If any)?
I use two classes. Class 1, with two threads in it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
public class MyThreadTest : EventArgs
{
private string _threadOutput = "";
private bool _stopThreads = false;
/// <summary>
/// Thread 1: Loop continuously,
/// Thread 1: Displays that we are in thread 1
/// </summary>
void DisplayThread1()
{
while (_stopThreads == false)
{
Console.WriteLine("Display Thread 1");
// Assign the shared memory to a message about thread #1
_threadOutput = "Hello Thread1";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user what thread we are in thread #1, and display shared memory
Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
}
}
/// <summary>
/// Thread 2: Loop continuously,
/// Thread 2: Displays that we are in thread 2
/// </summary>
void DisplayThread2()
{
while (_stopThreads == false)
{
Console.WriteLine("Display Thread 2");
// Assign the shared memory to a message about thread #2
_threadOutput = "Hello Thread2";
Thread.Sleep(1000); // simulate a lot of processing
// tell the user we are in thread #2
Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
}
}
void CreateThreads()
{
// construct two threads for our demonstration;
Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
Thread thread2 = new Thread(new ThreadStart(DisplayThread2));
// start them
thread1.Start();
thread2.Start();
}
public static void Main()
{
MyThreadTest StartMultiThreads = new MyThreadTest();
StartMultiThreads.CreateThreads();
}
}
I know there is some extra code but generally my goal is to simulate two threads.
The second class where I try to implement my delegate and eventually use a custom event for sending a message from the first thread to the second (at lest this is my final goal) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Class1
{
public delegate void ShowMyMessage(object sender, EventArgs e);
public event ShowMyMessage ShowIt;
// Invoke the ShowIt event; called whenever I like it :)
protected virtual void OnShowIt(EventArgs e)
{
if (ShowIt != null)
ShowIt(this, e);
}
}
class EventListener
{
private Class1 msg;
public EventListener(Class1 msg)
{
Class1 Message = msg;
// Add "ListChanged" to the Changed event on "List".
Message.ShowIt += new ShowMyMessage(MessageShowIt);
}
// This will be called whenever the list changes.
private void ListChanged(object sender, EventArgs e)
{
Console.WriteLine("This is called when the event fires.");
}
}
I know it’s not just a stupid mistake, but I need to know if this is the way to create an event and how I can finish successfully my job?
When you subscribe to an event you provide information about the method and the instance of an object. But not about a thread.
Events are always handled on the same thread where they were raised. Synchronously.
So events are not a mechanism to communicate between threads.