473,471 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

RaiseEvent

hi all.
in vb.net i can write RaiseEvent.
can i do it in C# also?
how?
thanks - shachar.

Nov 16 '05 #1
6 54429
Say you have an event

public event EventHandler Click;

event handlers for the EventHandler type events have the signature
void MyHandler( object sender, EventArgs e);

so at the point you wanted to raise the event you write call the event like a method ...

Click( this, EventArgs.Empty); // "this" is the sender and the event args is empty in this case

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<1b****************************@phx.gbl>

hi all.
in vb.net i can write RaiseEvent.
can i do it in C# also?
how?
thanks - shachar.
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 22/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2
Just to add, when creating general classes the pattern is to indirect the
call to the event with a protected virtual OnClick method that checks for
nullability (no one listening) of the Click event before raising it. This
helps derived classes to raise click event themeselves as they cannot raise
the event directly. Making OnClick virtual also allows the derived class to
intercept the event to do some work prior to any listeners recieving it:

public event EventHandler Click;
protected virtual void OnClick()
{
if( this.Click != null )
{
this.Click( this, EventArgs.Empty );
}
}

Events are raised by calling OnClick.
Regards
Lee

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:ez*************@TK2MSFTNGP11.phx.gbl...
Say you have an event

public event EventHandler Click;

event handlers for the EventHandler type events have the signature
void MyHandler( object sender, EventArgs e);

so at the point you wanted to raise the event you write call the event like a method ...
Click( this, EventArgs.Empty); // "this" is the sender and the event args is empty in this case
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<1b****************************@phx.gbl>
hi all.
in vb.net i can write RaiseEvent.
can i do it in C# also?
how?
thanks - shachar.
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 22/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #3
There is a problem with this (one that there has been a debate about
recently). When you perform the check like this:

if( this.Click != null )
{
this.Click( this, EventArgs.Empty );
}

There could be a race condition between the checking of Click against
null, and the call to Click. In order to get around this, you should really
do this:

// Store the reference to the delegate.
EventHandler e = this.Click;

// Perform the check.
if (e != null)
// Fire.
e(this, EventArgs.Empty);

However, JIT can get in the way here. It can see that the local
variable is really just a reference to Click, and optimize it away, leaving
you with the original condition (checking Click directly, which you don't
want). You could get around this (forcing the copy to be performed) by
passing Click to a method that will invoke it (the reference will be copied
on the stack, and it will not be optimized away.

HOWEVER (yes, again), JIT can strike again, inlining the method into the
local code, and then optimizing the parameter out (it's just another value
on the call stack now, like a variable).

If that method is defined as virtual though, it can not be inlined, and
the reference will be copied, and nothing will happen. It won't work in
this case because the reference is never copied to a variable that is not
optimized away.

In order to get around this, there are a few solutions. One is to
assign a default delegate to the event that points to empty code. This way,
you never have to check against null, since you always know there will be
one event listener. The anonymous methods introduced in v2.0 makes this
easy, by allowing the following:

// The Click event.
public event EventHandler Click = delegate{};

Another way to get around this is to define the event as volatile (it
will be allowed in .NET 2.0).

A third way would be to have a standard method makes the calls to fire
the delegate (dynamically), which would circumvent the optimizations (by
being virtual, or ultimately calling something which is virtual that passes
the delegate in as well as the parameters).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Lee Alexander" <lee@NoSpamPlease_Digita.com> wrote in message
news:u6**************@TK2MSFTNGP10.phx.gbl...
Just to add, when creating general classes the pattern is to indirect the
call to the event with a protected virtual OnClick method that checks for
nullability (no one listening) of the Click event before raising it. This
helps derived classes to raise click event themeselves as they cannot
raise
the event directly. Making OnClick virtual also allows the derived class
to
intercept the event to do some work prior to any listeners recieving it:

public event EventHandler Click;
protected virtual void OnClick()
{
if( this.Click != null )
{
this.Click( this, EventArgs.Empty );
}
}

Events are raised by calling OnClick.
Regards
Lee

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:ez*************@TK2MSFTNGP11.phx.gbl...
Say you have an event

public event EventHandler Click;

event handlers for the EventHandler type events have the signature
void MyHandler( object sender, EventArgs e);

so at the point you wanted to raise the event you write call the event

like a method ...

Click( this, EventArgs.Empty); // "this" is the sender and the event
args

is empty in this case

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<1b****************************@phx.gbl>

hi all.
in vb.net i can write RaiseEvent.
can i do it in C# also?
how?
thanks - shachar.
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 22/09/2004

[microsoft.public.dotnet.languages.csharp]


Nov 16 '05 #4
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
There is a problem with this (one that there has been a debate about
recently). When you perform the check like this:

if( this.Click != null )
{
this.Click( this, EventArgs.Empty );
}

There could be a race condition between the checking of Click against
null, and the call to Click. In order to get around this, you should really
do this:
<snip>
A third way would be to have a standard method makes the calls to fire
the delegate (dynamically), which would circumvent the optimizations (by
being virtual, or ultimately calling something which is virtual that passes
the delegate in as well as the parameters).


The fourth way - the way I prefer - is to use a lock (and preferrbly
not "this") as I show in
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

Using the no-op handler is a nice solution too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon,

A lock would definitely work, but from a maintinence ease-of-use
standpoint, it is a nightmare. The no-op handler is a better solution from
this perspective.

I think that the solution to have a static method (which in turn calls a
virtual method ultimately on an instance) is the best solution. It requires
minimal work on the part of the programmer, and it just works. All the
logic is encapsulated there.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
There is a problem with this (one that there has been a debate about
recently). When you perform the check like this:

if( this.Click != null )
{
this.Click( this, EventArgs.Empty );
}

There could be a race condition between the checking of Click against
null, and the call to Click. In order to get around this, you should
really
do this:


<snip>
A third way would be to have a standard method makes the calls to
fire
the delegate (dynamically), which would circumvent the optimizations (by
being virtual, or ultimately calling something which is virtual that
passes
the delegate in as well as the parameters).


The fourth way - the way I prefer - is to use a lock (and preferrbly
not "this") as I show in
http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml

Using the no-op handler is a nice solution too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
A lock would definitely work, but from a maintinence ease-of-use
standpoint, it is a nightmare. The no-op handler is a better solution from
this perspective.
I don't see it as a nightmare at all - you just put it in a single
method, and call that method when you want to raise the event. Often
you'd want that method anyway, so that it could be overridden. (Look at
all the OnXXX methods in the Windows Forms classes.)
I think that the solution to have a static method (which in turn
calls a virtual method ultimately on an instance) is the best
solution. It requires minimal work on the part of the programmer, and
it just works. All the logic is encapsulated there.


I disagree. It's not completely obvious to the reader how it solves the
threading problem, because it relies on what the JIT is able to do.
It's not totally beyond the bounds of possibility that there might be a
JIT which *does* inline virtual methods and then removes the
optimisation if the method is ever overridden, just like Hotspot does
for Java.

I suspect we'll have to agree to disagree though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guille | last post by:
Hi all! I'm having some weird behaviour in a .NET application i'm developing. I'll try to explain: I've created a Class that wraps an asynchronous socket. When connect callback is called, i...
8
by: Nicolas | last post by:
How do I do a RaiseEvent in csharp I'm ok in VB but csharp confused me a lot. ******* code ******** private FileSystemWatcher watcher = new FileSystemWatcher(); public delegate void...
2
by: Carl tam | last post by:
Hi everyone, I got a quite interesting problem myself and got stuck. I have an aspx page with a windows user control with it. in the Windows Control. I have a RaiseEvent statement, say...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
2
by: Lim | last post by:
I've developed a program that raise an event. This program works fine on a Windows 2000 Professional PC. However when I try to run the program on a Windos XP Professional PC, the program will not...
2
by: dmoonme | last post by:
I'm trying to rename some files in a directory. Pretty basic stuff - renaming the files works fine but the problem I have is updated the text in textbox. All I want to do is appendtext to a...
7
by: Onokiyo | last post by:
Hello, I have the code below and somehow the message from RaiseEvent doesn't pop up at all. Can someone help me please? '------CODE '------/form1.vb/VB2005/Framework20--------- Imports...
3
by: Martin | last post by:
Hi all, I'm having a problem when trying to raise an event in my custom control when a toolstripbutton enable state changes. The problem is that although the code to raise the event executes, my...
1
by: Terry Olsen | last post by:
I have a program with a couple of long running processes that i'm calling on a separate thread. When the process is completed, I want to raise an event to tell the main thread that it's done. I...
10
by: hzgt9b | last post by:
Using VS2005, VB.NET, I am developing a windows app. The application opens a couple of forms. While the forms are open I want to raise some events, such as logging errors - but I call RaiseEvent,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.