Click here to Skip to main content
15,921,793 members
Home / Discussions / C#
   

C#

 
Questionhow to add module in c#.net Pin
sarayumadhavan21-Nov-05 1:07
sarayumadhavan21-Nov-05 1:07 
AnswerRe: how to add module in c#.net Pin
S. Senthil Kumar21-Nov-05 4:51
S. Senthil Kumar21-Nov-05 4:51 
QuestionShould you add and remove event handlers willy-nilly? Pin
Gizz21-Nov-05 0:32
Gizz21-Nov-05 0:32 
AnswerRe: Should you add and remove event handlers willy-nilly? Pin
Robert Rohde21-Nov-05 1:06
Robert Rohde21-Nov-05 1:06 
GeneralRe: Should you add and remove event handlers willy-nilly? Pin
Gizz21-Nov-05 3:01
Gizz21-Nov-05 3:01 
GeneralRe: Should you add and remove event handlers willy-nilly? Pin
Robert Rohde21-Nov-05 7:48
Robert Rohde21-Nov-05 7:48 
AnswerRe: Should you add and remove event handlers willy-nilly? Pin
Charlie Williams21-Nov-05 7:32
Charlie Williams21-Nov-05 7:32 
AnswerRe: Should you add and remove event handlers willy-nilly? Pin
Leslie Sanford21-Nov-05 9:10
Leslie Sanford21-Nov-05 9:10 
Gizz quoted his supplier:
You should only ever add/remove an event handler once in most applications. It is not designed by Microsoft to have event handlers added and removed continuously as a method to controlling these things.


Well, I don't know about adding/removing event handlers "continuously," but I see nothing wrong with changing event handlers more than once. I've been exploring flow-based programming lately and have been experimenting with using events/delegates as a means of realizing it.

So I have no problem with adding/removing event handlers several times during the lifetime of an application as a means of controlling the flow of a program (or whatever). In fact, I think it's a pretty cool approach.

Gizz continued to quote his supplier:
This is because an event handler is usually dealt with in another thread (or how else could it be generated for the application)


If you're receiving event notifications from another thread, then thread-safety is an issue to be concerned about. but that has nothing to do with this:

and adding/removing notifications to the other thread can easily be the subject of racetrack timing errors.


Adding and removing event handlers to an event is thread safe.

When compiling a field-like event, the compiler automatically creates storage to hold the delegate and creates accessors for the event that add or remove event handlers to the delegate field. To be thread-safe, the addition or removal operations are done while holding the lock on the containing object for an instance event or the type object for a static event.

The C# Programming Language - 10.7.1

As far as firing an event, see Charlie Williams' reponse. There is a thread safety issue there.

-- modified at 15:10 Monday 21st November, 2005
QuestionHow to get embedded .jpeg from excel worksheet? Pin
Andrew Muliar21-Nov-05 0:30
Andrew Muliar21-Nov-05 0:30 
QuestionHow to minimize/maximize a borderless form from the taskbar PinPopular
tprashanth21-Nov-05 0:21
tprashanth21-Nov-05 0:21 
QuestionParse a word doc and update contents to database Pin
ranjithib120-Nov-05 22:14
ranjithib120-Nov-05 22:14 
AnswerRe: How to use C# to call a traditional unmanaged C++ DLL which has many functions with pointer parameters? Pin
Stanciu Vlad20-Nov-05 21:50
Stanciu Vlad20-Nov-05 21:50 
QuestionHow to use C# to call a traditional unmanaged C++ DLL which has many functions with pointer parameters? Pin
tsung-yu20-Nov-05 20:48
tsung-yu20-Nov-05 20:48 
AnswerRe: How to use C# to call a traditional unmanaged C++ DLL which has many functions with pointer parameters? Pin
RobstaHendricks23-Nov-09 5:53
RobstaHendricks23-Nov-09 5:53 
Question,F1 help in Windows App Pin
Stephen McAllister20-Nov-05 19:53
Stephen McAllister20-Nov-05 19:53 
Questionhow to download or upload a file using C# Pin
v.k.s20-Nov-05 17:09
v.k.s20-Nov-05 17:09 
AnswerRe: how to download or upload a file using C# Pin
Christian Graus20-Nov-05 17:15
protectorChristian Graus20-Nov-05 17:15 
QuestionGetChildAtPoint which is out of screen Pin
tsunsau71720-Nov-05 17:06
tsunsau71720-Nov-05 17:06 
QuestionC# inheritance Pin
ppp00120-Nov-05 16:53
ppp00120-Nov-05 16:53 
AnswerRe: C# inheritance Pin
Christian Graus20-Nov-05 17:10
protectorChristian Graus20-Nov-05 17:10 
AnswerRe: C# inheritance Pin
S. Senthil Kumar20-Nov-05 18:50
S. Senthil Kumar20-Nov-05 18:50 
QuestionAdding button to Menu Bar in Office 2003 Web Components Pin
ykoon20-Nov-05 16:25
ykoon20-Nov-05 16:25 
Questionquestion on Impersonation Pin
ekynox20-Nov-05 15:16
ekynox20-Nov-05 15:16 
AnswerRe: question on Impersonation Pin
ekynox20-Nov-05 18:09
ekynox20-Nov-05 18:09 
QuestionString.Replace with Escaped Characters Pin
Reanalyse20-Nov-05 15:11
Reanalyse20-Nov-05 15:11 

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.