How to use Monitor with Thread for nodes app

May 14 2012 3:38 PM

hi there, i create a tree there are 5 nodes and that are relevant each other.
ex: 
 Node 1 : childs : 2 and 3
        Node 2: childs : 1 and 4
        Node 3: childs 1 and 5
        Node 4: childs 2
        Node 5: childs 2

i created 5 thread at the same time.

 foreach (Node dugum in nodes)  {  try  {  ParameterizedThreadStart ths = new ParameterizedThreadStart(StartTh);  Thread th = new Thread(ths);  th.Priority = ThreadPriority.Highest;  th.Name = dugum.nodeid.ToString();  th.Start(dugum);  }  catch (ThreadStartException ex)  {  MessageBox.Show(ex.Message);  }  catch (ThreadInterruptedException ex)  {  MessageBox.Show(ex.Message);  }  }


 void StartTh(object values)  {  Node node = (Node)values;  foreach (Node child in node.childs)  {  listBox1.Items.Add("Nodeid :" + node.nodeid + " -> " + sendMesg(child));  }
 }


 private string sendMesg(Node child) {  string rslt= "";  lock (this)  {  try  {  if (!hashtable.ContainsKey(child.nodeid))  {  hashtable.Add(child.nodeid, child.energy);  Monitor.Wait(this);  }  Monitor.Pulse(this);  hashtable.Remove(child.nodeid);  rslt = child.nodeid.ToString();  }  catch (ThreadStartException ex)  {  MessageBox.Show(ex.Message);  }  catch (ThreadInterruptedException ex)  {  MessageBox.Show(ex.Message);  }  }  return rslt;  }


my question is i don't want 2 nodes send same child at the same times. one node send and other wait in queue then send message. I created some codes but i cant see well solution.
the output always appears 5 or 6 result in listbox. actually i want to see 8 message to response about sending.
How can i do this with monitor ?