threading

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

//

}

}

}
 
N

Nicholas Paldino [.NET/C# MVP]

Avi,

I didn't get a good look at your code but I think that the problem
arises from the fact that the thread is not set up for COM interop properly
(COM interop is used for the message queuing). I think that you should set
up the thread by setting the ApartmentState property of the thread to
ApartmentState.STA before any calls to the messaging components.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Avi Kadosh said:
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

//

}

}

}
 
A

Avi Kadosh

Hi,

Thanks for your replay.

I tried to define it with STA for the same result...

Also I don't use COM but the framework assamblies for queue ...

Thanks,

Avi

Nicholas Paldino said:
Avi,

I didn't get a good look at your code but I think that the problem
arises from the fact that the thread is not set up for COM interop properly
(COM interop is used for the message queuing). I think that you should set
up the thread by setting the ApartmentState property of the thread to
ApartmentState.STA before any calls to the messaging components.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Avi Kadosh said:
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

//

}

}

}

 
A

Avi Kadosh

I was able to narrow down the problem,

When the thread is started the events is fired with no reason, then since
there was no message in the queue and since I didn;t handle the exciption
the thread was destoyed.

any ideas, why the event was fired although there was nothing in the queue.

Avi Kadosh said:
Hi,

Thanks for your replay.

I tried to define it with STA for the same result...

Also I don't use COM but the framework assamblies for queue ...

Thanks,

Avi

message news:%[email protected]...
Avi,

I didn't get a good look at your code but I think that the problem
arises from the fact that the thread is not set up for COM interop properly
(COM interop is used for the message queuing). I think that you should set
up the thread by setting the ApartmentState property of the thread to
ApartmentState.STA before any calls to the messaging components.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Avi Kadosh said:
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

//

}

}

}


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top