先安装消息队列服务器
private static readonly string path = ".\\Private$\\myQueue";
private void Create()
{
if (!MessageQueue.Exists(path))
{
MessageQueue.Create(path);
}
}
private void Send()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
MessageQueue myQueue = new MessageQueue(path);
myQueue.Send(textBox1.Text);
stopwatch.Stop();
}
private void Display()
{
Task.Run(() =>
{
MessageQueue rcvMyQueue=new MessageQueue(path);
rcvMyQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
while (true)
{
var message = rcvMyQueue.Receive();
listBox1.BeginInvoke(new Action(() =>
{
listBox1.Items.Add(DateTime.Now.ToString("HH:mm:ss.fff")+":"+message.Body.ToString());
}));
}
});
}
private void Form1_Load(object sender, EventArgs e)
{
Create();
Display();
}