Click here to Skip to main content
15,904,416 members
Home / Discussions / C#
   

C#

 
GeneralRe: code complies blows sky high on button OK click Pin
Anonymous15-Sep-05 18:18
Anonymous15-Sep-05 18:18 
GeneralRe: code complies blows sky high on button OK click Pin
Christian Graus18-Sep-05 10:57
protectorChristian Graus18-Sep-05 10:57 
AnswerRe: code complies blows sky high on button OK click Pin
Guffa15-Sep-05 19:18
Guffa15-Sep-05 19:18 
Questioncan't get variable into Watch Window? Pin
Anonymous15-Sep-05 14:18
Anonymous15-Sep-05 14:18 
AnswerRe: can't get variable into Watch Window? Pin
Christian Graus15-Sep-05 14:20
protectorChristian Graus15-Sep-05 14:20 
QuestionProblem with database Pin
kavicky15-Sep-05 13:49
kavicky15-Sep-05 13:49 
AnswerRe: Problem with database Pin
Christian Graus15-Sep-05 14:23
protectorChristian Graus15-Sep-05 14:23 
QuestionEvent handlers in multi-threaded apps Pin
brucemo15-Sep-05 13:32
brucemo15-Sep-05 13:32 
I'm a C# noob so I apologize if I say something stupid or unclear.

I have a class that contains a thread that sits there in a loop, considering the world. Every once in a while, it decides that it needs to alert the world to some idea it has, so it calls its delegate function, sends information to all of its clients, who get the information via event handler functions that they've "+="'d to the delgate.

If this class didn't incorporate a thread, I would be done here, and everything would work perfectly. My class's clients would add a handler to the class's delegate when they feel like listening to it jabber, and they would periodically receive data and do whatever they want with it.

The terrible problem is that you can't ordinarily mess with controls if you aren't in the control's originating thread, so you need to use "Invoke".

If I understand correctly, one way to deal with this would be to have all the clients realize they are dealing with a class that is kind of toxic, and have all handlers that sign up with my class call "Invoke" whenever they are called. Essentially, the clients would realize that they are operating from within a foreign thread, and they would take appropriate precautions.

I don't like this solution, because the clients shouldn't have to know that they are being called from a different thread. They should just be able to add a handler and get data from the class, and everything should work.

Also, I could have some performance issues here, since future instances of my class might not use a thread, and I'd be stuck with all of this old ugly code that uses "Invoke".

Another way to deal with this is for the class itself to use "Invoke" on the delegate. I haven't thoroughly tested this, but it compiles and apparently runs, at least when there is only one client.

The drawback to this is that when I create the class, I have to pass in the form that created the class, so there will be an object to use to call invoke. This is ugly, and also I expect it would mess up if another thread added a handler to my class.

Question:

What is the best way to deal with events that are generated from within a thread, so that the clients work correctly, don't have to know that they are dealing with a thread, and don't have to pass difficult to explain parameters around?

Thank you,

bruce

AnswerRe: Event handlers in multi-threaded apps Pin
brucemo15-Sep-05 13:57
brucemo15-Sep-05 13:57 
GeneralRe: Event handlers in multi-threaded apps Pin
brucemo15-Sep-05 14:35
brucemo15-Sep-05 14:35 
AnswerRe: Event handlers in multi-threaded apps Pin
Judah Gabriel Himango15-Sep-05 15:27
sponsorJudah Gabriel Himango15-Sep-05 15:27 
AnswerRe: Event handlers in multi-threaded apps Pin
leppie15-Sep-05 16:12
leppie15-Sep-05 16:12 
GeneralRe: Event handlers in multi-threaded apps Pin
brucemo15-Sep-05 17:07
brucemo15-Sep-05 17:07 
GeneralRe: Event handlers in multi-threaded apps Pin
Andy Brummer15-Sep-05 17:38
sitebuilderAndy Brummer15-Sep-05 17:38 
GeneralRe: Event handlers in multi-threaded apps Pin
brucemo15-Sep-05 21:29
brucemo15-Sep-05 21:29 
QuestionA Specific Control Query Pin
udaan15-Sep-05 12:41
udaan15-Sep-05 12:41 
AnswerRe: A Specific Control Query Pin
Guffa15-Sep-05 13:46
Guffa15-Sep-05 13:46 
QuestionRichTextbox to send and receive data Pin
Ralf Hägenläuer15-Sep-05 10:09
Ralf Hägenläuer15-Sep-05 10:09 
QuestionDataSet Select statment Pin
zaboboa15-Sep-05 8:51
zaboboa15-Sep-05 8:51 
AnswerRe: DataSet Select statment Pin
Turtle Hand15-Sep-05 9:21
Turtle Hand15-Sep-05 9:21 
AnswerRe: DataSet Select statment Pin
Carl Mercier15-Sep-05 9:48
Carl Mercier15-Sep-05 9:48 
QuestionActiveReports +C# Pin
Agyeman15-Sep-05 8:33
Agyeman15-Sep-05 8:33 
Questionshort/easy question Pin
Lapje15-Sep-05 8:27
Lapje15-Sep-05 8:27 
AnswerRe: short/easy question Pin
Judah Gabriel Himango15-Sep-05 10:13
sponsorJudah Gabriel Himango15-Sep-05 10:13 
GeneralRe: short/easy question Pin
Lapje15-Sep-05 23:31
Lapje15-Sep-05 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.