A
Avi Kadosh
hi all
I am new to Csharp
I tried the following code
using System;
using System.Threading;
using System.Messaging;
namespace ConsoleApplication3
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class QueueWorker
{
private MessageQueue MQueue;
private int index;
public QueueWorker(int ix)
{
index = ix;
}
public void CreateQueue()
{
if (!MessageQueue.Exists(".\\private$\\" + index ))
{
MQueue = MessageQueue.Create(".\\private$\\" + index , false);
}
else
{
MQueue = new MessageQueue(".\\private$\\" + index,true);
}
MQueue.Formatter = new ActiveXMessageFormatter();
MQueue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MyReceiveCompleted);
MQueue.BeginReceive() ;
Console.WriteLine("Start Reciving on Queue " + index);
Thread.CurrentThread.Name = "My Queue Hadle Thread " + index;
}
private void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous Receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
// Display message information on the screen.
//Console.WriteLine("Message: " + (string)m.Body);
try
{
Console.WriteLine((string)m.Body);
}
catch
{
}
// Restart the asynchronous Receive operation.
mq.BeginReceive();
return;
}
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
QueueWorker[] a = new QueueWorker[30] ;
for(int i=0;i<30;i++)
{
a = new QueueWorker(i) ;
a.CreateQueue;
}
while(true)
{
System.Threading.Thread.Sleep(10000);
}
//
// TODO: Add code to start application here
//
}
}
}
it is working fine the problem is when I tried to change it to multithreaded
application (each queue worker is runing in it's own queue)
it create the thread but stop reciving events ... any ideas?
using System;
using System.Threading;
using System.Messaging;
namespace ConsoleApplication3
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class QueueWorker
{
private MessageQueue MQueue;
private int index;
public QueueWorker(int ix)
{
index = ix;
Thread myThread = new Thread(new ThreadStart(this.CreateQueue));
myThread.Start();
}
public void CreateQueue()
{
if (!MessageQueue.Exists(".\\private$\\" + index ))
{
MQueue = MessageQueue.Create(".\\private$\\" + index , false);
}
else
{
MQueue = new MessageQueue(".\\private$\\" + index,true);
}
MQueue.Formatter = new ActiveXMessageFormatter();
MQueue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MyReceiveCompleted);
MQueue.BeginReceive() ;
Console.WriteLine("Start Reciving on Queue " + index);
Thread.CurrentThread.Name = "My Queue Hadle Thread " + index;
}
private void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous Receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
// Display message information on the screen.
//Console.WriteLine("Message: " + (string)m.Body);
try
{
Console.WriteLine((string)m.Body);
}
catch
{
}
// Restart the asynchronous Receive operation.
mq.BeginReceive();
return;
}
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
QueueWorker[] a = new QueueWorker[30] ;
for(int i=0;i<30;i++)
{
a = new QueueWorker(i) ;
}
while(true)
{
System.Threading.Thread.Sleep(10000);
}
//
// TODO: Add code to start application here
//
}
}
}
I am new to Csharp
I tried the following code
using System;
using System.Threading;
using System.Messaging;
namespace ConsoleApplication3
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class QueueWorker
{
private MessageQueue MQueue;
private int index;
public QueueWorker(int ix)
{
index = ix;
}
public void CreateQueue()
{
if (!MessageQueue.Exists(".\\private$\\" + index ))
{
MQueue = MessageQueue.Create(".\\private$\\" + index , false);
}
else
{
MQueue = new MessageQueue(".\\private$\\" + index,true);
}
MQueue.Formatter = new ActiveXMessageFormatter();
MQueue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MyReceiveCompleted);
MQueue.BeginReceive() ;
Console.WriteLine("Start Reciving on Queue " + index);
Thread.CurrentThread.Name = "My Queue Hadle Thread " + index;
}
private void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous Receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
// Display message information on the screen.
//Console.WriteLine("Message: " + (string)m.Body);
try
{
Console.WriteLine((string)m.Body);
}
catch
{
}
// Restart the asynchronous Receive operation.
mq.BeginReceive();
return;
}
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
QueueWorker[] a = new QueueWorker[30] ;
for(int i=0;i<30;i++)
{
a = new QueueWorker(i) ;
a.CreateQueue;
}
while(true)
{
System.Threading.Thread.Sleep(10000);
}
//
// TODO: Add code to start application here
//
}
}
}
it is working fine the problem is when I tried to change it to multithreaded
application (each queue worker is runing in it's own queue)
it create the thread but stop reciving events ... any ideas?
using System;
using System.Threading;
using System.Messaging;
namespace ConsoleApplication3
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class QueueWorker
{
private MessageQueue MQueue;
private int index;
public QueueWorker(int ix)
{
index = ix;
Thread myThread = new Thread(new ThreadStart(this.CreateQueue));
myThread.Start();
}
public void CreateQueue()
{
if (!MessageQueue.Exists(".\\private$\\" + index ))
{
MQueue = MessageQueue.Create(".\\private$\\" + index , false);
}
else
{
MQueue = new MessageQueue(".\\private$\\" + index,true);
}
MQueue.Formatter = new ActiveXMessageFormatter();
MQueue.ReceiveCompleted += new
ReceiveCompletedEventHandler(MyReceiveCompleted);
MQueue.BeginReceive() ;
Console.WriteLine("Start Reciving on Queue " + index);
Thread.CurrentThread.Name = "My Queue Hadle Thread " + index;
}
private void MyReceiveCompleted(Object source,
ReceiveCompletedEventArgs asyncResult)
{
// Connect to the queue.
MessageQueue mq = (MessageQueue)source;
// End the asynchronous Receive operation.
Message m = mq.EndReceive(asyncResult.AsyncResult);
// Display message information on the screen.
//Console.WriteLine("Message: " + (string)m.Body);
try
{
Console.WriteLine((string)m.Body);
}
catch
{
}
// Restart the asynchronous Receive operation.
mq.BeginReceive();
return;
}
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
QueueWorker[] a = new QueueWorker[30] ;
for(int i=0;i<30;i++)
{
a = new QueueWorker(i) ;
}
while(true)
{
System.Threading.Thread.Sleep(10000);
}
//
// TODO: Add code to start application here
//
}
}
}