annotate Messaging/Server/Listeners/MsmqListener.cs @ 65:5d0c5cc83a7a

Click event handler added
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sun, 22 Apr 2012 14:21:33 +0100
parents 7d9de5746f18
children
rev   line source
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
1 using System;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
2 using System.Messaging;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
3 using Common.Messages;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
4 using GalaSoft.MvvmLight.Messaging;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
5
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
6 namespace Server.Listeners
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
7 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
8 public class MsmqListener : IListener
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
9 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
10 public bool IsListening { get; set; }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
11
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
12 private readonly MessageQueue _mq;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
13
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
14 public MsmqListener(string queueName)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
15 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
16 _mq = MessageQueue.Exists(queueName)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
17 ? new MessageQueue(queueName)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
18 : MessageQueue.Create(queueName);
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
19
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
20 _mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
21 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
22
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
23 public void Start()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
24 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
25 if (IsListening) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
26
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
27 Messenger.Default.Send(new MsmqLogMessage() { Body = "Opening listener" });
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
28 IsListening = true;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
29 while (IsListening)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
30 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
31 try
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
32 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
33 var message = _mq.Receive().Body;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
34 Messenger.Default.Send(new MsmqClientMessage());
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
35 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
36 catch (MessageQueueException mqe)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
37 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
38 Messenger.Default.Send(new MsmqLogMessage() { Body = mqe.Message });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
39 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
40 catch (Exception e)
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
41 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
42 Messenger.Default.Send(new MsmqLogMessage() { Body = e.Message });
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
43 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
44 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
45 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
46
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
47 public void Stop()
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
48 {
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
49 if (!IsListening) return;
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
50 _mq.Close();
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
51 IsListening = false;
31
7d9de5746f18 Working version
adminsh@apollo
parents: 29
diff changeset
52 Messenger.Default.Send(new MsmqLogMessage() { Body = "Listener closed." });
29
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
53 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
54 }
9919ee227c93 msmq added
adminsh@apollo
parents:
diff changeset
55 }