首先定义一个事件参数,一个委托,事件参数包括你要发出的信息。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(...);  
        }
}
不管有多少客户端,只要预订这个事件即可。当不需要得到通知时,就移除这个预订。

解决方案 »

  1.   

    如果是订单处理为异步的可以考虑使用消息队列MSMQ。楼上的适合同步处理模式,即双方程序必须同时运行。MSMQ类似邮件,但使用更简单,同时支持广播,事务,安全等,与COM+集成也比较方便。
      

  2.   


    hi, firefight()您能详细解绍MSMQ吗?或那里有例子!
      

  3.   

    http://www.c-sharpcorner.com/Code/2004/July/MSMQ.asp
      

  4.   

    MSMQ 消息队列!
    是不是一样要定时扫描!
      

  5.   

    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)
    {
    }
    }