Csharp/C Sharp by API/System.Messaging/MessageQueue

Материал из .Net Framework эксперт
Версия от 12:11, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

MessageQueue.Create(String value)

  

using System;
using System.Messaging;

class Program {
    static void Main(string[] args) {
        using (MessageQueue queue = MessageQueue.Create(@".\MyNewPublicQueue")) {
            Console.WriteLine("Queue created:");
            Console.WriteLine("Path: {0}", queue.Path);
            Console.WriteLine("FormatName: {0}", queue.FormatName);
        }
        Console.ReadLine();
    }
}


MessageQueue.Exists(String value)

  
using System;
using System.Messaging;
[Serializable]
class MyMessage {
    private string data;
    public MyMessage(string data) {
        this.data = data;
    }

    public override string ToString() {
        return data;
    }
}
class Program {
    static void Main(string[] args) {
        try {
            if (!MessageQueue.Exists(@".\Private$\MyPrivateQueue")) {
                MessageQueue.Create(@".\Private$\MyPrivateQueue");
            }
            MessageQueue queue = new MessageQueue(@".\Private$\MyPrivateQueue");
            queue.Formatter = new BinaryMessageFormatter();
            queue.Send("Sample Message", "Label");
            MyMessage m = new MyMessage("xxx");
            queue.Send(m, "Binary Test");
        } catch (MessageQueueException ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


MessageQueue.Send

  
using System;
using System.Messaging;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    MessageQueue txq1 = new MessageQueue( @".\Private$\txq1" );
    for ( int i =1; i <= 20; i++ )
    {
      Message msgOut = new Message();
      msgOut.Body = "Message " + i;
      txq1.Send( msgOut, MessageQueueTransactionType.Single );
    }
  }
}


new MessageQueue

  
using System;
using System.Messaging;
class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    MessageQueue txq1 = new MessageQueue( @".\Private$\txq1" );
    for ( int i =1; i <= 20; i++ )
    {
      Message msgOut = new Message();
      msgOut.Body = "Message " + i;
      txq1.Send( msgOut, MessageQueueTransactionType.Single );
    }
  }
}