PC Review


Reply
Thread Tools Rate Thread

Cross-Thread Exception

 
 
Relaxin
Guest
Posts: n/a
 
      20th Mar 2008
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.

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Mar 2008
Relaxin <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
Relaxin
Guest
Posts: n/a
 
      21st Mar 2008

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Mar 2008
Relaxin <(E-Mail Removed)> wrote:
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.


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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
Relaxin
Guest
Posts: n/a
 
      21st Mar 2008

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Relaxin <(E-Mail Removed)> wrote:
>> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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.

>
> 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...

 
Reply With Quote
 
Barry Kelly
Guest
Posts: n/a
 
      21st Mar 2008
Relaxin wrote:

> 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

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Mar 2008
Relaxin <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
Relaxin
Guest
Posts: n/a
 
      22nd Mar 2008

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!

 
Reply With Quote
 
Relaxin
Guest
Posts: n/a
 
      22nd Mar 2008

"Barry Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Relaxin wrote:
>
>> 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.).
>


Thanks Barry for that great piece of information!

Thanks for helping!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ShowDialog - Cross-thread operation not valid: Control'CheckAccountInfo' accessed from a thread other than the thread it was createdon. Tom C Microsoft C# .NET 9 20th Feb 2008 08:15 PM
BackgroundWorker and ListView cross-thread exception Jazza Microsoft Dot NET Framework Forms 1 16th Feb 2008 01:42 PM
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on Joe Microsoft C# .NET 4 12th Mar 2007 09:59 AM
Cross Thread Exception after reading Asynchronous Serial Port Mo Microsoft C# .NET 5 12th Nov 2006 09:34 PM
'cross-thread operation not valid' exception / multithreaded compo =?Utf-8?B?SGFpcmxpcERvZzU4?= Microsoft Dot NET 11 24th Apr 2006 03:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:04 PM.