Groups | Blog | Home
all groups > dotnet general > march 2008 >

dotnet general : Cross-Thread Exception


Relaxin
3/20/2008 12:29:44 PM
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.
Relaxin
3/20/2008 6:37:50 PM

[quoted text, click to view]
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?
Jon Skeet [C# MVP]
3/20/2008 9:49:38 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Jon Skeet [C# MVP]
3/21/2008 2:02:54 AM
[quoted text, click to view]

And how are you doing that? What call is throwing the exception? What's
the calling program doing?

[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Relaxin
3/21/2008 2:07:11 AM

[quoted text, click to view]
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...
Barry Kelly
3/21/2008 2:43:40 PM
[quoted text, click to view]

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

--
Jon Skeet [C# MVP]
3/21/2008 4:46:36 PM
[quoted text, click to view]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Relaxin
3/21/2008 9:47:25 PM

[quoted text, click to view]

I already found it, no thanks to you!
Relaxin
3/22/2008 2:21:15 AM

[quoted text, click to view]

Thanks Barry for that great piece of information!

Thanks for helping!
AddThis Social Bookmark Button