首先定义一个事件参数,一个委托,事件参数包括你要发出的信息。public class NotifyEventArgs : EventArgs
{
public string message;
public NotifyEventArgs(string str)
{
message = str;
}
}public delegate void NotifyEventHandler(object obj, NotifyEventArgs);public class Body
{
public event NotifyEventHandler NotifyEvent;
private void SendNotify()
{
if(NotifyEvent!=null)
{
NotifyEvent(this,new NotifyEventArgs("请生产十颗原子弹,午后二时交货.");
}
}
}public class WordShop
{
public Body body;
void Init()
{
body.NotifyEvent += new NotifyEventHandler(...);
}
}
不管有多少客户端,只要预订这个事件即可。当不需要得到通知时,就移除这个预订。
{
public string message;
public NotifyEventArgs(string str)
{
message = str;
}
}public delegate void NotifyEventHandler(object obj, NotifyEventArgs);public class Body
{
public event NotifyEventHandler NotifyEvent;
private void SendNotify()
{
if(NotifyEvent!=null)
{
NotifyEvent(this,new NotifyEventArgs("请生产十颗原子弹,午后二时交货.");
}
}
}public class WordShop
{
public Body body;
void Init()
{
body.NotifyEvent += new NotifyEventHandler(...);
}
}
不管有多少客户端,只要预订这个事件即可。当不需要得到通知时,就移除这个预订。
解决方案 »
- 两个字符串比大小
- 招聘人数和应聘人数关系
- 简单的问题
- C# ListView控件显示图片的例子
- C# Httpwebquest获取日文网页内容时有乱码,求助!
- javaScript 令人郁闷的问题
- 我有一个tabctrl,这个tabctrl有好几个tabpage,每个tabpage上面有好几个textbox,我想使用fontdialog,使用fontdialog的时候,需要知道当前所选择文字的textbox是吧,问题是:
- 8.0是2005还是2008?
- 请问如下代码有无内存泄漏?为什么?
- 问一下CLR对象释放的问题..
- 求助~~~快啊~~~知道的帮我来看看!!
- ep.ShowDialog(this) == DialogResult.OK
hi, firefight()您能详细解绍MSMQ吗?或那里有例子!
是不是一样要定时扫描!
楼主要使用什么语言开发?如果使用C#或VB比较简单,VC相对复杂一点。还有MSMQ只支持微软的操作系统,如果是异构环境可以考虑其它消息中间件,例如BEA MessageQ, IBM MQ等。资料网上多得时,看MSDN也就足够了。
以下是一个C#的简单例子
使用MSMQ
选择项目-》添加引用,浏览并选择System.Messaging.dll
加入using System.Messaging
发送
string hostName=System.Net.Dns.GetHostName();
System.Messaging.Message m=new System.Messaging.Message();
m.Label="Service";
m.Body="abc";
System.Messaging.MessageQueue mq=new System.Messaging.MessageQueue();
mq.Path=hostName+"\\private$\\csharp";
mq.Send(m);
m.Dispose();
mq.Dispose();
接收
System.Messaging.MessageQueue MQ=new System.Messaging.MessageQueue();
MQ.Path=m_MachineName+"\\private$\\"+m_QueueName;
System.Messaging.Message Message=new System.Messaging.Message();
while(true)
{
try
{
System.Threading.Thread.Sleep(100);
Message=MQ.Receive(new System.TimeSpan(0,0,0,1));
FileStream output=new FileStream("c:\\theone.txt",FileMode.OpenOrCreate,FileAccess.Write);
BinaryWriter bw=new BinaryWriter(output);
bw.Write(Message.Label);
bw.Close();
output.Close();
}
catch(ThreadInterruptedException e)
{
System.Console.WriteLine("Exit Thread");
Message.Dispose();
MQ.Dispose();
break;
}
catch(Exception GenericException)
{
}
}