Hello guys!
I have a problem In my current Project. My application to works, needs communication with external system ( receiving and sending messages ).
All communication is already coded, but I think it needs to be rewritten.
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 ).
In short, described class looks like this :
Since “System” class is kind of adapter to external system API, it should be” locked” here right ?
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.
I believe whole principle is wrong. Could somebody write some suggestions, how to rewrite this ?
I have a problem In my current Project. My application to works, needs communication with external system ( receiving and sending messages ).
All communication is already coded, but I think it needs to be rewritten.
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 ).
In short, described class looks like this :
class MQueueEventArgs : EventArgs { public MQueueEventArgs(string message) { this.Message = message; } public string Message { get; private set; } } class MQueue { // [...] public event EventHandler<MQueueEventArgs> NewMessagesArrived; private void Timer_Elapsed(object sender, ElapsedEventArgs e) { if (System.AreNewMessages) { for (int i = 0; i < 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); } // [...] }
Since “System” class is kind of adapter to external system API, it should be” locked” here right ?
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.
I believe whole principle is wrong. Could somebody write some suggestions, how to rewrite this ?