以下是我用於收聽Windows窗體消息的代碼。收聽msmq隊列
我注意到,當我點擊send
它發送消息給MyQueue,但當時我希望事件mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
應該被調用,但它不是,換句話說,我試圖從Windows窗體訂閱MyQueue 。只是想知道我是否遺漏了代碼中的某些東西:
public class Form1 : System.Windows.Forms.Form
{
public System.Messaging.MessageQueue mq;
public static Int32 j=0;
public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
// Queue Creation
if(MessageQueue.Exists(@".\Private$\MyQueue"))
mq = new System.Messaging.MessageQueue(@".\Private$\MyQueue");
else
mq = MessageQueue.Create(@".\Private$\MyQueue");
mq.ReceiveCompleted += new ReceiveCompletedEventHandler(mq_ReceiveCompleted);
mq.BeginReceive();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnMsg_Click(object sender, System.EventArgs e)
{
// SendMessage(Handle, 1, 0, IntPtr.Zero);
System.Messaging.Message mm = new System.Messaging.Message();
mm.Body = txtMsg.Text;
mm.Label = "Msg" + j.ToString();
j++;
mq.Send(mm);
}
void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
//throw new NotImplementedException();
}
private void btnRcv_Click(object sender, System.EventArgs e)
{
System.Messaging.Message mes;
string m;
try
{
mes = mq.Receive(new TimeSpan(0, 0, 3));
mes.Formatter = new XmlMessageFormatter(new String[] {"System.String,mscorlib"});
m = mes.Body.ToString();
}
catch
{
m = "No Message";
}
MsgBox.Items.Add(m.ToString());
}
}
您需要學習發佈**相關代碼** - 不只是您的整個代碼文件!減少你的代碼到那些對於這個問題非常重要的方法/行 - 不要發佈所有Windows窗體生成的代碼 - 它不會爲你的文章增加任何價值..... – 2010-07-14 16:40:12
對不起馬克。 – 2010-07-14 17:35:53