-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (67 loc) · 3.15 KB
/
Copy pathProgram.cs
File metadata and controls
69 lines (67 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Diagnostics;
using System.Messaging;
using System.Threading;
using System.Threading.Tasks;
namespace Task1.Consumer
{
class Program
{
private const int SEC1 = 1000;
const string queuePath = @".\Private$\MSMQ-Task1";
static void Main()
{
Console.WriteLine("'Consumer' started");
using (MessageQueue mq = MessageQueueHelper.GetMessageQueue(queuePath))
{
bool exit;
do
{
Task.Run(() =>
{
do
{
Message message = null;
try
{
message = mq.Receive(new TimeSpan(0, 0, seconds: 3));
}
catch (MessageQueueException)
{
Console.WriteLine("not found message in queue");
}
if (message != null)
{
ProducerMessage customerMessage = null;
try
{
message.Formatter = new XmlMessageFormatter(new[] {typeof (ProducerMessage)});
customerMessage = (ProducerMessage) message.Body;
}
catch (Exception e)
{
Console.WriteLine("failed with '{0}'", e.Message);
}
if (customerMessage != null)
{
Guid taskId = Guid.NewGuid();
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Received message '{0}, {1}'", customerMessage.Text, customerMessage.Duration);
Console.WriteLine("{0} starting", taskId);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Thread.Sleep(customerMessage.Duration * SEC1);
Console.WriteLine("{0} completed, duration = {1}", taskId, Math.Round(stopwatch.ElapsedMilliseconds/(double)(SEC1)));
Console.ResetColor();
}
}
} while (true);
});
//Console.Write("enter command or 'exit' > ");
string command = Console.ReadLine() ?? "";
exit = String.IsNullOrWhiteSpace(command) || command.ToLower() == "exit";
} while (!exit);
}
}
}
}