Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
Basically, I'm using the class of .Net MessageQueue, I send a Message with FileName to a Queue, but I've received many errors from the Machines that consume the Queue like "File doesn't exist" or "FileNotFoundException". The machines supposed to delete the files.

If someone familiar with this code:

C#
static void Main()
{
   for (int i = 0; i < 10; i++)
       ThreadPool.QueueUserWorkItem(new WaitCallback(ListenerMessage));
}

private void ListenerMessage(object o)
{
MessageQueue queueSouce = new MessageQueue("otherMachine\otherQueue");
queueSouce.Formatter = new XmlMessageFormatter(new Type[] { typeof(XmlDocument) });

queueSouce.ReceiveCompleted += new ReceiveCompletedEventHandler(queueSouce_ReceiveCompleted);
queueSouce.BeginReceive();
}

void queueSouce_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
 ...
}


My theory in here is I'm using many threads in many machines connected with MessageQueue that start duplicate my messages, and when two computers receive the same message one of them will throw a exception that "file is using by another process" or "the file not found", etc

Someone can help me ?
Posted
Updated 16-Sep-13 11:25am
v2
Comments
Jeankininho 17-Sep-13 8:15am    
Anybody knows ?
Mehdy Moini 17-Sep-13 12:33pm    
It depend on what you do in queueSouce_ReceiveCompleted, if you do something like editing the file, so two machines can't modify one file at the same time, i guess you may delete the file , as one of the exceptions is :the file not found
Jeankininho 17-Sep-13 12:50pm    
Hi Mehdy Moini, thanks for answer, what I'm doing in this event is this:
private void queueSouce_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
System.Messaging.MessageQueue mq = (System.Messaging.MessageQueue)sender;
mq.EndReceive(e.AsyncResult);

File.Delete((e.Message.Body as XmlDocument).ChildNodes[0].Attributes["FileName"].Value);

mq.BeginReceive();
}
Do you think 2 threads can read the same message before the "mq.EndReceive(e.AsyncResult);" ?


1 solution

Imagine one thread is completed and another thread is not, so thread one, after endReceive , will delete the file when second thread have a message queue with a file name which doesn't point to a file, because first thread delete that.
 
Share this answer
 
v2
Comments
Jeankininho 17-Sep-13 13:14pm    
This make a sense, but I didn't think this could happen. Thanks Mehdy Moini. By the way, do you know how can I treat this error ?
Mehdy Moini 17-Sep-13 13:26pm    
your welcome jean,is it possible to check if the file exists then delete?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900