Reputation: 35557
Can a custom event be created for any object method?
To do this do I just use the following syntax?:
myObject.myMethod +=new EventHandler(myNameEvent);
The following code has prompted this question:
private void btRunProcessAndRefresh_Click(object sender,EventArgs e)
{
myProcess =new Process();
myProcess.StartInfo.FileName = @"c:\ConsoleApplication4.exe";
myProcess.Exited += new EventHandler(MyProcessExited);
myProcess.EnableRaisingEvents =true;
myProcess.SynchronizingObject =this;
btRunProcessAndRefresh.Enabled =false;
myProcess.Start();
}
Upvotes: 37
Views: 151260
Reputation: 3952
Based on @ionden's answer, the call to the delegate could be simplified using null propagation since C# 6.0.
Your code would simply be:
class MyClass {
public event EventHandler MyEvent;
public void Method() {
MyEvent?.Invoke(this, EventArgs.Empty);
}
}
Use it like this:
MyClass myObject = new MyClass();
myObject.MyEvent += myObject_MyEvent;
myObject.Method();
Upvotes: 8
Reputation: 2972
Yes, provided you have access to the object definition and can modify it to declare the custom event
public event EventHandler<EventArgs> ModelChanged;
And normally you'd back this up with a private method used internally to invoke the event:
private void OnModelChanged(EventArgs e)
{
if (ModelChanged != null)
ModelChanged(this, e);
}
Your code simply declares a handler for the declared myMethod
event (you can also remove the constructor), which would get invoked every time the object triggers the event.
myObject.myMethod += myNameEvent;
Similarly, you can detach a handler using
myObject.myMethod -= myNameEvent;
Also, you can write your own subclass of EventArgs
to provide specific data when your event fires.
Upvotes: 8
Reputation: 176896
Yes you can do like this :
Creating advanced C# custom events
or
The Simplest C# Events Example Imaginable
public class Metronome
{
public event TickHandler Tick;
public EventArgs e = null;
public delegate void TickHandler(Metronome m, EventArgs e);
public void Start()
{
while (true)
{
System.Threading.Thread.Sleep(3000);
if (Tick != null)
{
Tick(this, e);
}
}
}
}
public class Listener
{
public void Subscribe(Metronome m)
{
m.Tick += new Metronome.TickHandler(HeardIt);
}
private void HeardIt(Metronome m, EventArgs e)
{
System.Console.WriteLine("HEARD IT");
}
}
class Test
{
static void Main()
{
Metronome m = new Metronome();
Listener l = new Listener();
l.Subscribe(m);
m.Start();
}
}
Upvotes: 35
Reputation: 14919
Yes you can create events on objects, here is an example;
public class Foo
{
public delegate void MyEvent(object sender, object param);
event MyEvent OnMyEvent;
public Foo()
{
this.OnMyEvent += new MyEvent(Foo_OnMyEvent);
}
void Foo_OnMyEvent(object sender, object param)
{
if (this.OnMyEvent != null)
{
//do something
}
}
void RaiseEvent()
{
object param = new object();
this.OnMyEvent(this,param);
}
}
Upvotes: 14
Reputation: 12776
Declare the class containing the event:
class MyClass {
public event EventHandler MyEvent;
public void Method() {
OnEvent();
}
private void OnEvent() {
if (MyEvent != null) {
MyEvent(this, EventArgs.Empty);
}
}
}
Use it like this:
MyClass myObject = new MyClass();
myObject.MyEvent += new EventHandler(myObject_MyEvent);
myObject.Method();
Upvotes: 45
Reputation: 3996
You need to declare your event in the class from myObject :
public event EventHandler<EventArgs> myMethod; //you should name it as an event, like ObjectChanged.
then myNameEvent is the callback to handle the event, and it can be in any other class
Upvotes: 4