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.
I want safe thread reading in such a manner that shouldn't be blocked.
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.
}
}
Guest UserPosted Mar 14, 2011, 8:39 AM
Amit PathakPosted Mar 14, 2011, 1:09 AM
Sam HobbsPosted Mar 12, 2011, 6:22 PM
Guest UserPosted Mar 12, 2011, 5:28 PM
Sam HobbsPosted Mar 12, 2011, 3:44 PM
I think the important thing is that BackgroundWorker is probably the best solution. Look for articles in this web site about that.
I don't understand why the program reads the messages only when the receive button is clicked. First, though, go ahead and read the articles about BackgroundWorker. Then if you have questions, tell us if the program would work if it started reading messages at the beginning of the program and then continued to read messages or if there is a reason why the messages must be read only when the receive button is clicked.
Suthish NairPosted Mar 12, 2011, 1:48 PM
Guest UserPosted Mar 11, 2011, 8:57 AM
Thread.Sleep(10);
This causes that thread to stop processing for 10ms. This allows other processes and threads to continue processing and reduces the CPU utilization of the receivemessages thread.
You can adjust that 10ms to whatever you feel is best for performance.