1
public partial class _Default : System.Web.UI.Page
2
{
3
private string queuePath = @".\Private$\mydemoqueues";
4
protected void Page_Load(object sender, EventArgs e)
5
{
6
//异步获取消息
7
GetQueueMessage();
8
}
9
protected void Button1_Click(object sender, EventArgs e)
10
{
11
MessageQueue mq;
12
if(MessageQueue.Exists(@".\Private$\mydemoqueues"))
13
{
14
mq=new MessageQueue(@".\Private$\mydemoqueues");
15
}
16
else
17
{
18
mq = MessageQueue.Create(@".\Private$\mydemoqueues");
19
}
20
BinaryWriter writer=new BinaryWriter(new MemoryStream());
21
writer.Write(this.TextBox1.Text.Trim());
22
Message msg=new Message();
23
msg.BodyStream = writer.BaseStream;
24
msg.Label = "发送信息,飘渺峰测试";
25
mq.Send(msg);
26
27
}
28
protected void Button2_Click(object sender, EventArgs e)
29
{
30
MessageQueue mq;
31
Message msg=new Message();
32
string result;
33
if (MessageQueue.Exists(queuePath))
34
{
35
mq=new MessageQueue(queuePath);
36
}
37
else
38
{
39
mq = MessageQueue.Create(queuePath);
40
}
41
try
42
{
43
mq.MaximumQueueSize = 1000000;
44
msg = mq.Receive(new TimeSpan(0, 0, 3));
45
BinaryReader br=new BinaryReader(msg.BodyStream);
46
result=new string(br.ReadChars(Convert.ToInt32(msg.BodyStream.Length)));
47
this.TextBox2.Text = result;
48
mq.Close();
49
}
50
catch (Exception)
51
{
52
Response.Write("没有消息可以接收!");
53
}
54
}
55
/// <summary>
56
/// 异步获取消息队列
57
/// </summary>
58
private void GetQueueMessage()
59
{
60
MessageQueue mq=new MessageQueue(queuePath);
61
mq.Formatter = new XmlMessageFormatter(new Type[] {typeof (string)});
62
mq.PeekCompleted+=new PeekCompletedEventHandler(MyMessageCompleted);
63
mq.BeginPeek();
64
return;
65
}
66
67
private void MyMessageCompleted(object sender, PeekCompletedEventArgs e)
68
{
69
MessageQueue mq = (MessageQueue) sender;
70
Message msg = mq.EndPeek(e.AsyncResult);
71
BinaryReader reader=new BinaryReader(msg.BodyStream);
72
string temp=new string(reader.ReadChars(Convert.ToInt32(msg.BodyStream.Length)));
73
//Response.Write("这是异步队列信息:"+temp);
74
this.TextBox2.Text = temp;
75
mq.BeginPeek();
76
return;
77
}
78
79
private void SendMessage()
80
{
81
MessageQueue mq;
82
if (MessageQueue.Exists(@".\Private$\mydemoqueues"))
83
{
84
mq = new MessageQueue(@".\Private$\mydemoqueues");
85
}
86
else
87
{
88
mq = MessageQueue.Create(@".\Private$\mydemoqueues");
89
}
90
BinaryWriter writer = new BinaryWriter(new MemoryStream());
91
writer.Write("sdfsdfsd");
92
Message msg = new Message();
93
msg.BodyStream = writer.BaseStream;
94
msg.Label = "发送信息,飘渺峰测试";
95
mq.Send(msg);
96
}

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

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96
