本文整理汇总了C#中System.Messaging.MessageQueue.Send方法的典型用法代码示例。如果您正苦于以下问题:C# MessageQueue.Send方法的具体用法?C# MessageQueue.Send怎么用?C# MessageQueue.Send使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Messaging.MessageQueue
的用法示例。
在下文中一共展示了MessageQueue.Send方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Messaging;
namespace MyProject
{
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
//**************************************************
// Provides an entry point into the application.
//
// This example sends a message to a queue.
//**************************************************
public static void Main()
{
// Create a new instance of the class.
MyNewQueue myNewQueue = new MyNewQueue();
// Send a message to a queue.
myNewQueue.SendMessage();
return;
}
//**************************************************
// Sends a message to a queue.
//**************************************************
public void SendMessage()
{
// Connect to a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\\myQueue");
// Send a message to the queue.
if (myQueue.Transactional == true)
{
// Create a transaction.
MessageQueueTransaction myTransaction = new
MessageQueueTransaction();
// Begin the transaction.
myTransaction.Begin();
// Send the message.
myQueue.Send("My Message Data.", myTransaction);
// Commit the transaction.
myTransaction.Commit();
}
else
{
myQueue.Send("My Message Data.");
}
return;
}
}
}
示例2: Main
//引入命名空间
using System;
using System.Messaging;
namespace MyProject
{
/// <summary>
/// Provides a container class for the example.
/// </summary>
public class MyNewQueue
{
//**************************************************
// Provides an entry point into the application.
//
// This example sends and receives a message from
// a transactional queue.
//**************************************************
public static void Main()
{
// Create a new instance of the class.
MyNewQueue myNewQueue = new MyNewQueue();
// Send a message to a queue.
myNewQueue.SendMessageTransactional();
// Receive a message from a queue.
myNewQueue.ReceiveMessageTransactional();
return;
}
//**************************************************
// Sends a message to a queue.
//**************************************************
public void SendMessageTransactional()
{
// Connect to a queue on the local computer.
MessageQueue myQueue = new
MessageQueue(".\\myTransactionalQueue");
// Send a message to the queue.
if (myQueue.Transactional == true)
{
// Create a transaction.
MessageQueueTransaction myTransaction = new
MessageQueueTransaction();
// Begin the transaction.
myTransaction.Begin();
// Send the message.
myQueue.Send("My Message Data.", myTransaction);
// Commit the transaction.
myTransaction.Commit();
}
return;
}
//**************************************************
// Receives a message containing an Order.
//**************************************************
public void ReceiveMessageTransactional()
{
// Connect to a transactional queue on the local computer.
MessageQueue myQueue = new
MessageQueue(".\\myTransactionalQueue");
// Set the formatter.
myQueue.Formatter = new XmlMessageFormatter(new Type[]
{typeof(String)});
// Create a transaction.
MessageQueueTransaction myTransaction = new
MessageQueueTransaction();
try
{
// Begin the transaction.
myTransaction.Begin();
// Receive the message.
Message myMessage = myQueue.Receive(myTransaction);
String myOrder = (String)myMessage.Body;
// Display message information.
Console.WriteLine(myOrder);
// Commit the transaction.
myTransaction.Commit();
}
catch (MessageQueueException e)
{
// Handle nontransactional queues.
if (e.MessageQueueErrorCode ==
MessageQueueErrorCode.TransactionUsage)
{
Console.WriteLine("Queue is not transactional.");
}
// Else catch other sources of MessageQueueException.
// Roll back the transaction.
myTransaction.Abort();
}
// Catch other exceptions as necessary, such as
// InvalidOperationException, thrown when the formatter
// cannot deserialize the message.
return;
}
}
}
示例3: MessageQueue
// Connect to a transactional queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Send the message.
queue.Send(msg, MessageQueueTransactionType.Single);
示例4: MessageQueue
// Connect to a queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Send the message.
queue.Send(msg, "Example Message Label");
示例5: MessageQueue
// Connect to a transactional queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Create a message queuing transaction.
MessageQueueTransaction transaction = new MessageQueueTransaction();
try
{
// Begin a transaction.
transaction.Begin();
// Send the message to the queue.
queue.Send(msg, "Example Message Label", transaction);
// Commit the transaction.
transaction.Commit();
}
catch(System.Exception e)
{
// Cancel the transaction.
transaction.Abort();
// Propagate the exception.
throw e;
}
finally
{
// Dispose of the transaction object.
transaction.Dispose();
}
示例6: MessageQueue
// Connect to a transactional queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Send the message.
queue.Send(msg, "Example Message Label",
MessageQueueTransactionType.Single);
示例7: Main
//引入命名空间
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 );
}
}
}