intethread communication

  • Thread starter Thread starter Lore Leuneog
  • Start date Start date
L

Lore Leuneog

Hello

I'm looking for a way to programm two threads coninuously "talking" to
eachother. All I found was Asynchrone Delegates. But I don't want to have
one asynchone Callback. I need something like a two way queue between two
threads but I don't know how to implement this. Does anyone have a hint or
a link?

Thank you
Sincerely
Lore
 
i wrote a little piece of code to let two threads put messages into a queue and they can keep looking at the queue to process the messages designated for the

not sure if this is what you are talking about ..

using System
using System.Collections
using System.Threading

namespace TwoThreadQu

/// <summary
/// Summary description for Class1
/// </summary
class Class

/// <summary
/// The main entry point for the application
/// </summary
[STAThread
static void Main(string[] args

Class1 c = new Class1()

Thread wt1 = new Thread(new ThreadStart(c.Worker1))
wt1.Start()

Thread wt2 = new Thread(new ThreadStart(c.Worker2))
wt2.Start()


private void GetWork(string sID

lock(m_Que

if(m_Que.Count>0

Message s = m_Que.Peek() as Message

if( s.ID==sID

s = m_Que.Dequeue() as Message

Console.WriteLine(AppDomain.GetCurrentThreadId() + ": " + s.Msg)





private void SetWork(string reciever, string sender
{
Message s = new Message()

s.ID = reciever

s.Msg = "from " + sender + " to " + reciever + " at " + DateTime.Now.ToLongTimeString()

lock(m_Que
{
m_Que.Enqueue(s)



private void Worker1(

while(true

GetWork("worker1")

SetWork("worker2", "worker1")

Thread.Sleep(1000)



private void Worker2(

while(true

GetWork("worker2")

SetWork("worker1", "worker2")

Thread.Sleep(1000)



private Queue m_Que = new Queue()


public class Messag

public string ID

public string Msg
 
Back
Top