Cross-Thread Exception

R

Relaxin

How do you handle the cross-thread issues if you don't have a UI?

I'm writing a class that's using the MSMQ and MSMQ notifies the class when a
new message has arrived.
But it notifies you on a new thread.

I need to notify a class on the main thread that a new message has arrived,
but I get the cross-thread exception.

I've tried many different things, including delegates, but I still get this
error.

All of my googling turns up ways to handle it if you have a UI, which I
don't.

Help!!

P.S. Using VS2008, .NET 3.5 on Vista Business.
 
J

Jon Skeet [C# MVP]

Relaxin said:
How do you handle the cross-thread issues if you don't have a UI?

I'm writing a class that's using the MSMQ and MSMQ notifies the class when a
new message has arrived.
But it notifies you on a new thread.

I need to notify a class on the main thread that a new message has arrived,
but I get the cross-thread exception.

I've tried many different things, including delegates, but I still get this
error.

All of my googling turns up ways to handle it if you have a UI, which I
don't.

It would help if you'd say what you're trying to do with the message
when you get the exception.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
R

Relaxin

Jon Skeet said:
It would help if you'd say what you're trying to do with the message
when you get the exception.
I'm not be anything with the message, I'm just trying to notify the calling
program, thru a delegate, that a message has arrived.

So do you know anything about Cross-Thread exceptions at all and can offer
some help?
 
J

Jon Skeet [C# MVP]

Relaxin said:
I'm not be anything with the message, I'm just trying to notify the calling
program, thru a delegate, that a message has arrived.

And how are you doing that? What call is throwing the exception? What's
the calling program doing?
So do you know anything about Cross-Thread exceptions at all and can offer
some help?

Yes, I do. But you haven't given us enough information about what
you're doing. Again, a short but complete program would help.
 
R

Relaxin

Jon Skeet said:
And how are you doing that? What call is throwing the exception? What's
the calling program doing?
I'm doing it thru a delegate like I said!
The delegate is throwing the exception, can't you see that!
Waiting for the delegate notification just like I explained!

WOW!
I guess they give MVP's to anyone!

What a moron!

Bye...and of course, I don't need your type of help, so I'm now done with
you.

Good Day...
 
B

Barry Kelly

Relaxin said:
How do you handle the cross-thread issues if you don't have a UI?

You need a synchronization point. You can't hijack a random thread and
have it pay attention to you; it needs to wait explicitly at some point
so that you can communicate with it.

Apps with UIs have a synchronization point for the main thread in the
form of the message loop. The message loop waits for messages. Other
threads can communicate with it by sending a message. Control.Invoke
works by doing this.

For apps without UIs, you need to set up the synchronization point
yourself. You can do that by implementing a message loop, or a
producer-consumer queue (Google that - there's a trivial textbook
implementation possible using monitors, often one of the first things
taught in college courses when threading is first discussed), or other
methods of cross-thread communication. For example, the main thread
could block until a background thread wakes it up, using e.g.
ManualResetEvent or monitors (Monitor.Wait, Monitor.Pulse, etc.).

-- Barry
 
J

Jon Skeet [C# MVP]

Relaxin said:
I'm doing it thru a delegate like I said!
The delegate is throwing the exception, can't you see that!
Waiting for the delegate notification just like I explained!

WOW!
I guess they give MVP's to anyone!

What a moron!

Bye...and of course, I don't need your type of help, so I'm now done
with you.

Good Day...

Good luck waiting for help if you're not going to give any more
information.

I'll be busy helping people who are actually willing to explain their
situation in more detail.
 
R

Relaxin

Jon Skeet said:
Good luck waiting for help if you're not going to give any more
information.

I'll be busy helping people who are actually willing to explain their
situation in more detail.

I already found it, no thanks to you!
 
R

Relaxin

Barry Kelly said:
You need a synchronization point. You can't hijack a random thread and
have it pay attention to you; it needs to wait explicitly at some point
so that you can communicate with it.

Apps with UIs have a synchronization point for the main thread in the
form of the message loop. The message loop waits for messages. Other
threads can communicate with it by sending a message. Control.Invoke
works by doing this.

For apps without UIs, you need to set up the synchronization point
yourself. You can do that by implementing a message loop, or a
producer-consumer queue (Google that - there's a trivial textbook
implementation possible using monitors, often one of the first things
taught in college courses when threading is first discussed), or other
methods of cross-thread communication. For example, the main thread
could block until a background thread wakes it up, using e.g.
ManualResetEvent or monitors (Monitor.Wait, Monitor.Pulse, etc.).

Thanks Barry for that great piece of information!

Thanks for helping!
 

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