Mungkin kode di bawah ini akan berguna bagi seseorang untuk mendapatkan pengenalan cepat ke MSMQ .
Jadi untuk memulai, saya sarankan Anda membuat 3 aplikasi dalam sebuah solusi:
- MessageTo (Formulir Windows) Tambahkan 1 tombol.
- MessageFrom (Formulir Windows) Tambahkan 1 kotak teks kaya.
- MyMessage (Perpustakaan Kelas) Tambahkan 1 kelas.
Cukup salin kode sebelumnya dan coba. Pastikan bahwa MSMQ diinstal pada MS Windows Anda dan proyek 1 dan 2 memiliki referensi System.Messaging
.
1. Tombol MessageTo (Formulir Windows) Tambahkan 1.
using System;
using System.Messaging;
using System.Windows.Forms;
namespace MessageTo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
#region Create My Own Queue
MessageQueue messageQueue = null;
if (MessageQueue.Exists(@".\Private$\TestApp1"))
{
messageQueue = new MessageQueue(@".\Private$\TestApp1");
messageQueue.Label = "MyQueueLabel";
}
else
{
// Create the Queue
MessageQueue.Create(@".\Private$\TestApp1");
messageQueue = new MessageQueue(@".\Private$\TestApp1");
messageQueue.Label = "MyQueueLabel";
}
#endregion
MyMessage.MyMessage m1 = new MyMessage.MyMessage();
m1.BornPoint = DateTime.Now;
m1.LifeInterval = TimeSpan.FromSeconds(5);
m1.Text = "Command Start: " + DateTime.Now.ToString();
messageQueue.Send(m1);
}
}
}
2. MessageFrom (Formulir Windows) Tambahkan 1 kotak teks kaya.
using System;
using System.ComponentModel;
using System.Linq;
using System.Messaging;
using System.Windows.Forms;
namespace MessageFrom
{
public partial class Form1 : Form
{
Timer t = new Timer();
BackgroundWorker bw1 = new BackgroundWorker();
string text = string.Empty;
public Form1()
{
InitializeComponent();
t.Interval = 1000;
t.Tick += T_Tick;
t.Start();
bw1.DoWork += (sender, args) => args.Result = Operation1();
bw1.RunWorkerCompleted += (sender, args) =>
{
if ((bool)args.Result)
{
richTextBox1.Text += text;
}
};
}
private object Operation1()
{
try
{
if (MessageQueue.Exists(@".\Private$\TestApp1"))
{
MessageQueue messageQueue = new MessageQueue(@".\Private$\TestApp1");
messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MyMessage.MyMessage) });
System.Messaging.Message[] messages = messageQueue.GetAllMessages();
var isOK = messages.Count() > 0 ? true : false;
foreach (System.Messaging.Message m in messages)
{
try
{
var command = (MyMessage.MyMessage)m.Body;
text = command.Text + Environment.NewLine;
}
catch (MessageQueueException ex)
{
}
catch (Exception ex)
{
}
}
messageQueue.Purge(); // after all processing, delete all the messages
return isOK;
}
}
catch (MessageQueueException ex)
{
}
catch (Exception ex)
{
}
return false;
}
private void T_Tick(object sender, EventArgs e)
{
t.Enabled = false;
if (!bw1.IsBusy)
bw1.RunWorkerAsync();
t.Enabled = true;
}
}
}
3. MyMessage (Class Library) Tambahkan 1 kelas.
using System;
namespace MyMessage
{
[Serializable]
public sealed class MyMessage
{
public TimeSpan LifeInterval { get; set; }
public DateTime BornPoint { get; set; }
public string Text { get; set; }
}
}
Nikmati :)