<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Principle of external-messages listening thread' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Principle of external-messages listening thread' posted on the 'C#' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Sat, 18 May 2013 15:59:52 -0700</pubDate>
    <lastBuildDate>Sat, 18 May 2013 15:59:52 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Principle of external-messages listening thread</title>
      <link>http://www.programmersheaven.com/mb/csharp/431272/431272/principle-of-external-messages-listening-thread/</link>
      <description>Hello guys!&lt;br /&gt;
&lt;br /&gt;
I have a problem In my current Project. My application to works, needs communication with external system ( receiving and sending messages ).&lt;br /&gt;
&lt;br /&gt;
All communication is already coded, but I think it needs to be rewritten.&lt;br /&gt;
At the moment, I’m using System.Timers.Timer with delay of 20ms between interrupts to check if new data arrived. On “ElapsedEventHandler” I’m asking external system about new commands, and if any available I’m executing them in timer thread ( pretty bad, isn’t it ? ). I have to make sure that my application is able respond to the system, and send some results. ( both command and answer are simple xml ).&lt;br /&gt;
&lt;br /&gt;
In short, described class looks like this :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt; 
    class MQueueEventArgs : EventArgs
    {
    	public MQueueEventArgs(string message)
    	{
    		this.Message = message;
    	}
    
    	public string Message { get; private set; }
    }
    
    class MQueue
    {
    	// [...]
    
    	public event EventHandler&amp;lt;MQueueEventArgs&amp;gt; NewMessagesArrived;
    
    	private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    	{
    		if (System.AreNewMessages)
    		{
    			for (int i = 0; i &amp;lt; System.MessagesCount; i++)
    			{
    				Enqueue(System.GetNextMessage());
    				OnChanged();
    			}
    		}
    	}
    
    	protected void OnChanged()
    	{
    		if (NewMessagesArrived != null)
    			NewMessagesArrived(this, new MQueueEventArgs(Dequeue()));
    	}
    
    	public void Put(string message)
    	{
    		System.SendMessage(message);
    	}
    	// [...]
    }&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Since “System” class is kind of adapter to external system API, it should be” locked” here right ? &lt;br /&gt;
I can imagine situation when  system will send us more than 8 messages at once, and while “ElapsedEventHandler” is executed, other logic could call Put(message) method.&lt;br /&gt;
 &lt;br /&gt;
I believe whole principle is wrong. Could somebody write some suggestions, how to rewrite this ? &lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/csharp/431272/431272/principle-of-external-messages-listening-thread/</guid>
      <pubDate>Tue, 12 Feb 2013 03:10:57 -0700</pubDate>
      <category>C#</category>
    </item>
  </channel>
</rss>