【发布时间】:2011-07-13 07:12:38
【问题描述】:
我想读取 MSMQ,其中以字节为单位的队列数据和在 1 分钟内生成的队列数约为 1500。因此,如果连续读取,队列 cpu 会持续 30%。过了一段时间,它停止了。我需要大量阅读队列长达 4 小时.. 所以我希望以不应该被阻塞的方式安全地读取线程。 其实我不擅长穿线,所以请你帮帮我..
目前我正在以这种方式阅读
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.
}
}
【问题讨论】:
标签: c# multithreading thread-safety msmq