问题
i want to read the MSMQ where queue data in byte and numbers of queues generated in 1 mins about 1500. so if read continuously queue cpu goes on 30%. and after some time it stopped. i need to read queue in high volume upto 4 hrs.. so i want safe thread reading in such a manner that shouldn't be block. actually i am not good about threading so can you please help me out..
currently i am reading in this manner
bool ProcessStatus; //process
Thread _UDPthreadConsme;
private void btn_receive_Click(object sender, EventArgs e)
{
if (MessageQueue.Exists(@".\private$\myquelocal"))
{
ThreadStart _processrcs = new ThreadStart(receivemessages);
_UDPthreadConsme = new Thread(_processrcs);
ProcessStatus = true;
_UDPthreadConsme.Start();
}
}
private void receivemessages()
{
MessageBox.Show("Start");
while (ProcessStatus)
{
try
{
// Connect to the a queue on the local computer.
MessageQueue myQueue = new MessageQueue(@".\private$\myquelocal");
System.Messaging.Message[] myMessagecount = myQueue.GetAllMessages();
if (myMessagecount.Length <= 0)
return;
myQueue.Formatter = new BinaryMessageFormatter();
// Receive and format the message.
System.Messaging.Message myMessage = myQueue.Receive();
byte[] buffer = (byte[])myMessage.Body;
// here i convert buffer to its related structure and then insert the values in database sqlserver.
}
}
回答1:
I would rewrite the code like this
private void receivemessages()
{
Console.WriteLine("Start");
MessageQueue myQueue = new MessageQueue(@".\private$\myquelocal");
while (ProcessStatus)
{
try
{
// Waits 100 millisec for a message to appear in queue
System.Messaging.Message msg = myQueue.Receive(new TimeSpan(0, 0, 0, 0, 100));
// Take care of message and insert data into database
}
catch (MessageQueueException)
{
// Ignore the timeout exception and continue processing the queue
}
}
}
回答2:
This is an example of a class that runs on the console and reads a queue asynchronously. This is the safest and fastest way to do this. However note that depending on where you're running this, you'll still need to have some kind of locking mechanism if you're doing things like updating text boxes with the message body or something like that.
public sealed class ConsoleSurrogate {
MessageQueue _mq = null;
public override void Main(string[] args) {
_mq = new MessageQueue(@".\private$\my_queue", QueueAccessMode.Receive);
_mq.ReceiveCompleted += new ReceiveCompletedEventHandler(_mq_ReceiveCompleted);
_mq.Formatter = new ActiveXMessageFormatter();
MessagePropertyFilter filter = new MessagePropertyFilter();
filter.Label = true;
filter.Body = true;
filter.AppSpecific = true;
_mq.MessageReadPropertyFilter = filter;
this.DoReceive();
Console.ReadLine();
_mq.Close();
}
void _mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e) {
_mq.EndReceive(e.AsyncResult);
Console.WriteLine(e.Message.Body);
this.DoReceive();
}
private void DoReceive() {
_mq.BeginReceive();
}
}
来源:https://stackoverflow.com/questions/5269951/want-to-read-msmq-message-fast-using-safe-thread-in-c-sharp-net