Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
foreach (Node node in nodes)  {  
try  {  
ParameterizedThreadStart ths = new ParameterizedThreadStart(StartTh);  
Thread th = new Thread(ths);  
th.Priority = ThreadPriority.Highest;  
th.Name = node .nodeid.ToString(); 
 th.Start(node );  
}  
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 ?

like that see this image
Posted
Updated 15-May-12 2:51am
v3
Comments
Sergey Alexandrovich Kryukov 15-May-12 12:03pm    
Why with Monitor? Why would you make thread priority Highest, ever?
Why using Hashtable (since v.2.0, inclusive, use one of generic classes)?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900