I would like to create event actions to notify other classes when something happened. So my current flow looks like this
For testing purposes I created this code
Program.cs
Instantiate the first class and call a method from it (constructor is fine).
internal class Program
{
private static void Main(string[] args)
{
First f = new First();
}
}
First.cs
Instantiate the second class and call a method from it (constructor is fine). Listen for an event of the second class when some data has changed.
internal class First
{
public First()
{
// ...
Second s = new Second();
s.Updated += OnSecondUpdated;
}
private void OnSecondUpdated()
{
Console.WriteLine("Done");
Console.ReadLine();
}
}
Second.cs
Instantiate the third class and call a method from it (constructor is fine). Listen for an event of the third class when some data has changed and raise the own one.
internal class Second
{
public event Action Updated;
public Second()
{
// ...
Third t = new Third();
t.Updated += OnThirdUpdated;
}
private void OnThirdUpdated()
{
// ...
Updated();
}
}
Third.cs
Raise an event when some data has changed.
internal class Third
{
public event Action Updated;
public Third()
{
// ...
Updated();
}
}
Unfortunately the event variables are null. How can I instantiate these variables properly?
The problem here is that you're trying to do this in the constructor, where at that time nothing has (yet) been assigned to the Updated event. You can "solve" this by checking for null:
internal class Third
{
public event Action Updated;
public Third()
{
// ...
if(Updated != null)
Updated();
}
}
But it wont mean your code now "works" as you only assign the event a handler after constructor has been called:
Third t = new Third();
t.Updated += OnThirdUpdated;
So one possible solution for this pattern is to NOT do this raising of the event in the constructor, and instead defer the logic to another method.
internal class Third
{
public event Action Updated;
public Third()
{
}
public void Init()
{
// ...
if(Updated != null)
Updated();
}
}
Third t = new Third();
t.Updated += OnThirdUpdated;
t.Init();
You call the Update() before the classes can subscribe to the events, due to the constructor of the underlying object being called first. I changed it so that the constructor takes the related class and subscribes the event itself.
internal class First
{
public First()
{
Second s = new Second(this);
}
internal void OnSecondUpdated()
{
Console.WriteLine("Done");
Console.ReadLine();
}
}
internal class Second
{
public event Action Updated;
public Second(First f)
{
Updated += f.OnSecondUpdated;
Third t = new Third(this);
}
internal void OnThirdUpdated()
{
Updated();
}
}
internal class Third
{
public event Action Updated;
public Third(Second s)
{
Updated += s.OnThirdUpdated;
Updated();
}
}
Related
I want to be able to have an object add one of its methods to an EventHandler that is passed to it and give said method the ability to remove itself from the EventHandler.
public class EventRaiser {
public event EventHandler event1
public event EventHandler event2
public void fire() {
event1?.Invoke(this, null);
event2?.Invoke(this, null);
}
}
public class EventSubscriber {
EventHandler eh;
public EventSubscriber(EventHandler eh) {
this.eh = eh;
eh += receive;
}
public void receive(object obj, EventArgs data) {
// Do stuff.
if(condition) eh -= receive;
}
}
public class MainClass {
public void Main() {
EventRaiser er = new EventRaiser();
EventSubscriber es1 = new EventSubscriber(er.event1);
EventSubscriber es2 = new EventSubscriber(er.event2);
er.fire();
}
}
The above code does not compile as I cannot even pass er.event1 or er.event2 to EventSubscriber ("The event can only appear in the left hand side of +=..."). Removing the event keyword from the EventHandlers fixes this issue but unsubscribing does not work properly. Is there a way to make this work? Use pointers maybe?
The problem here comes from you passing an EventHandler, not the list holding the delegates behind it itsself. Basically the "list of method pointers" to your handlers.
As you can see, in the declaration of event1 you have the keyword event, which is missing when you pass it somewhere else.
Unfortunately you cannot extract the "delegate holder" of an event easily.
Basically at the time you want to register your handler to an event you somehow need a compile time reference to it, in order to be able to += and -= to it.
You could do the following:
public class EventRaiser
{
public delegate void Event1(string args);
public List<Event1> handlers = new List<Event1>();
public void register(Event1 handler)
{
handlers.Add(handler);
}
public void unregister(Event1 handler)
{
handlers.Remove(handler);
}
public void fire()
{
handlers.ForEach(handler => handler("myEventArgs"));
}
}
public class EventSubscriber
{
Action<Event1> registerAction;
Action<Event1> unregisterAction;
public EventSubscriber(Action<Event1> register, Action<Event1> unregister)
{
registerAction = register;
unregisterAction = unregister;
registerAction(receive);
}
public void receive(string args)
{
// Do stuff.
unregisterAction(receive);
}
}
public class MainClass
{
public void Main()
{
EventRaiser er = new EventRaiser();
EventSubscriber es1 = new EventSubscriber(er.register, er.unregister);
er.fire();
}
}
I want an abstract class that raises an event, this event will be raised by the concrete class.
What I want is when I use another class to listen to these events the signature of the delegate should have the concrete type not the abstract, I don't want to cast it.
For the moment I have come up with this solution. It works but I don't find it particularly clever especially because of the "STUPID, DOESN'T MAKE SENSE......" part.
Here is my solution :
public delegate void ClassAEventHandler<TClassA>(TClassA classA) where TClassA : ClassA;
//Abstract class that raise Event
public abstract class ClassA<TClassA> : where TClassA : ClassA
{
public event ClassAEventHandler<TClassA> onClassEventRaised;
private TClassA eventClassA;
public void registerEventClass(TClassA classA)
{
this.eventClassA = classA;
}
public void raiseClassEvent()
{
this.onClassEventRaised(this.eventClassA);
}
}
// Exemple of concrete type
public class ClassB : ClassA<ClassB> // <------ IT SEEMS DUMB
{
public void action()
{
//Do something then raise event
this.raiseClassEvent();
}
public void saySomething() {};
}
// Exemple of concrete type
public class ClassC : ClassA<ClassC> // <------ IT SEEMS DUMB
{
public void command()
{
//Do something then raise event
this.raiseClassEvent();
}
public void destroySomething() {};
}
//Class that listen to the event raised
public class MyEventListener
{
private ClassB classB;
private ClassC classC;
public MyEventListener()
{
this.classB = new ClassB();
this.classB.registerEventClass(this.classB); // <------ STUPID, DOESN'T MAKE SENSE......
this.classB.onClassEventRaised += classB_onClassEventRaised;
this.classC = new ClassC();
this.classC.registerEventClass(this.classC); // <------ STUPID, DOESN'T MAKE SENSE......
this.classC.onClassEventRaised += classC_onClassEventRaised;
}
public void classB_onClassEventRaised(ClassB classB)
{
classB.saySomething();
}
public void classC_onClassEventRaised(ClassC classC)
{
classC.destroySomething();
}
//What i don't want
/*
public void classB_onClassEventRaised(ClassA classA)
{
((classB)classA).saySomething();
}
*/
}
First of all, you're not following regular event design in .NET.
Instead of implementing your own delegate, use EventHandler<TArgs>, and create a derived class of EventArgs.
Your CustomEventArgs should have a T generic parameter:
public class CustomEventArgs<T> where T : A
{
private readonly T _instance;
public CustomEventArgs(T instance)
{
_instance = instance;
}
public T Instance { get { return _instance; } }
}
Also, don't implement a custom way of registering events. If you want to encapsulate how handlers are added to the event, you need to use event accessors.
Finally, you could implement your classes as follows:
public class A<T> where T : A
{
private event EventHandler<CustomEventArgs<T>> _someEvent;
// An event accessor acts like the event but it can't be used
// to raise the event itself. It's just an accessor like an special
// event-oriented property (get/set)
public event EventHandler<CustomEventArgs<T>> SomeEvent
{
add { _someEvent += value; }
remove { _someEvent -= value; }
}
protected virtual void RaiseSomeEvent(CustomEventArgs<T> args)
{
// If C# >= 6
_someEvent?.Invoke(this, args);
// Or in C# < 6
// if(_someEvent != null) _someEvent(this, args);
}
}
public class B : A<B>
{
public void DoStuff()
{
// It's just about raising the event accessing the whole
// protected method and give an instance of CustomEventArgs<B>
// passing current instance (i.e. this) to CustomEventArgs<T>
// constructor.
RaiseSomeEvent(new CustomEventArgs<B>(this));
}
}
Now, if you try to handle SomeEvent, you'll get the CustomEventArgs<B> typed as B instead of A:
B b = new B();
b.SomeEvent += (sender, args) =>
{
// args.Instance is B
B instance = args.Instance;
};
b.DoStuff(); // Raises SomeEvent internally
tl;dr
Implementing Class:
public Main()
{
Foo foo = new Foo();
foo.OnBarOneResponse += foo_OnBarOneResponse;
foo.OnBarTwoResponse += foo_OnBarTwoResponse;
foo.FetchBarOne();
}
void foo_OnBarOneResponse(String response)
{
// Called successfully.
this.foo.FetchBarTwo();
}
void foo_OnBarTwoResponse(String response)
{
// Never called :(
}
Foo.cs
private MyJavascriptInjector _javascriptInjector = new MyJavascriptInjector();
public delegate void OnBarOneResponseHandler(String response);
public delegate void OnBarTwoResponseHandler(String response);
public event OnBarOneResponseHandler OnBarOneResponse = delegate { };
public event OnBarTwoResponseHandler OnBarTwoResponse = delegate { };
private void _onBarOneResponse(String response)
{
// Called Successfully
OnBarOneResponse(response);
}
private void _onBarTwoResponse(String response)
{
// Never called :(
OnBarTwoResponse(response);
}
public Foo()
{
webBrowser.ObjectForScripting = _javascriptInjector;
_javascriptInjector.OnBarOneResponse += _onBarOneResponse;
_javascriptInjector.OnBarTwoResponse += _onBarTwoResponse;
webBrowser.Navigate("http://myurl", null, new Byte[0], myHeaders");
}
public void FetchBarOne()
{
webBrowser.InvokeScript("fetchBarOne");
}
public void FetchBarTwo()
{
webBrowser.InvokeScript("fetchBarTwo");
}
MyJavascriptInjector.cs
[System.Runtime.InteropServices.ComVisible(true)]
public class MyJavascriptInjector
{
public delegate void OnBarOneResponseHandler(string response);
public delegate void OnBarTwoResponseHandler(string response);
public event OnBarOneResponseHandler OnBarOneResponse;
public event OnBarTwoResponseHandler OnBarTwoResponse;
public void OnBarOneResponse(String response)
{
// Called successfully!
OnBarOneResponse(response);
}
public void OnBarTwoResponse(String response)
{
// ALSO CALLED SUCCESSFULLY BUT WHEN CALLING THIS Foo.cs event NEVER GETS FIRED.
// IT GETS LOST SOMEWHERE BETWEEN HERE and Foo.cs!
OnBarTwoResponse(response);
}
}
=================
I have an object Foo that has two methods on it, FetchBarOne and FetchBarTwo.
Each method has an event on it, OnBarOneResponse and OnBarTwoResponse.
The implementing class registers Foo's events in its constructor using the notation "+=" and defines a callback function for each: foo_OnBarOneResponse(String response) and foo_OnBarTwoResponse(String response).
PROBLEM:
The implementing class observes the following:
Calls this.foo.FetchBarOne();
foo_OnBarOneResponse(String response) is fired at a later time.
In this callback, implementing class immediately calls this.foo.FetchBarTwo();
foo_OnBarTwoResponse(String response) never fires.
MORE INFORMATION:
Foo has wrapped WebBrowser and is calling InvokeScript to execute javascript in the loaded webpage. This webpage has many javascript functions on it, including FetchBarOne and FetchBarTwo on it. When debugging, FetchBarTwo is called and it successfully responds with data. However, after WebBrowser returns the data successfully, when Foo calls its internal OnBarTwoResponseHandler event delegate that was registered by the implementing class, it gets "lost" somewhere in between - even though it is not null at it clearly has a reference to it.
FAILED ATTEMPTS OF FIXING ISSUE
Implementing class tried using Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(() => this.foo.FetchBarTwo())); to try and call it from the UI thread. No success.
If anyone has any thoughts on this matter, I would be most grateful. Thanks!
The this.foo field is NOT the same instance as the foo variable.
From your example:
public Main()
{
Foo foo = new Foo();
foo.OnBarOneResponse += foo_OnBarOneResponse;
foo.OnBarTwoResponse += foo_OnBarTwoResponse;
foo.FetchBarOne();
}
void foo_OnBarOneResponse(String response)
{
// Called successfully.
this.foo.FetchBarTwo();
}
Try using your field (which was not included in your example):
private Foo foo;
public Main()
{
this.foo = new Foo();
this.foo.OnBarOneResponse += foo_OnBarOneResponse;
this.foo.OnBarTwoResponse += foo_OnBarTwoResponse;
this.foo.FetchBarOne();
}
I was given a generic API class, that contains a custom event which always needs to be invoked by the main UI thread.
My job is to banish these invocation call from the custom class, to make it "painless".
It should be synchronized like the default events in WinForms (eg the Timer "Elapsed" event, which also needs no invocation when it published values to a text box)
Is it possible to solve this, since the custom class needs to know where to invoke?
Here's the (important part of the) code:
public class ContactSensorHelper
{
public event OnReleaseStateChanged ReleaseStateChanged;
public delegate void OnReleaseStateChanged(ContactSensorEventArgs e);
private ContactSensorEventArgs.ReleaseState recentReleaseState;
public void ReportStateChanged()
{
if (ReleaseStateChanged != null)
ReleaseStateChanged(new ContactSensorEventArgs()
{
State = recentReleaseState
});
}
public class ContactSensorEventArgs : EventArgs
{
//......
public ReleaseState State { get; set; }
//......
public enum ReleaseState
{
FullReleased,
PartlyReleased,
NotReleased
}
}
}
The call from main UI:
public void SensorInit()
{
//....
sensorHelper.ReleaseStateChanged += releaseStateChanged;
//....
}
private void releaseStateChanged(ContactSensorEventArgs e)
{
//example
textBox1.Text = e.State.ToString(); // Thread exception (obviously)
}
Does anybody have me a hint to start?
You could do this by using your own event calling, and storing a reference to the thread, when the event is attached.
With the event add/remove syntax, you can have the caller attach to the event like before, but internally you store a list, with a reference to the thread (using an AsyncOperation) and the delegate to be called (used a Tuple containing both in the example)
Below is an example. I tested it, and it worked as expected when testing, but you might have to add some locking of the list to make it thread safe in case events are added/removed simultaneously.
public class ContactSensorHelper:IDisposable
{
public delegate void OnReleaseStateChanged(ContactSensorEventArgs e);
private ContactSensorEventArgs.ReleaseState recentReleaseState;
public void ReportStateChanged()
{
if (statechangedList.Count > 0)
{
var e = new ContactSensorEventArgs()
{
State = recentReleaseState
};
statechangedList.ForEach(t =>
t.Item1.Post(o => t.Item2((ContactSensorEventArgs)o), e));
}
}
List<Tuple<AsyncOperation, OnReleaseStateChanged>> statechangedList = new List<Tuple<AsyncOperation,OnReleaseStateChanged>>();
public event OnReleaseStateChanged ReleaseStateChanged
{
add
{
var op = AsyncOperationManager.CreateOperation(null);
statechangedList.Add(Tuple.Create(op, value));
}
remove
{
var toremove = statechangedList.Where(t => t.Item2 == value).ToArray();
foreach (var t in toremove)
{
t.Item1.OperationCompleted();
statechangedList.Remove(t);
}
}
}
public void Dispose()
{
statechangedList.ForEach(t => t.Item1.OperationCompleted());
statechangedList.Clear();
}
public class ContactSensorEventArgs : EventArgs
{
//......
public ReleaseState State { get; set; }
//......
public enum ReleaseState
{
FullReleased,
PartlyReleased,
NotReleased
}
}
}
im trying to learn delegates and events in c#, i understand that an event is some sort of a wrapper for a delegate and a delegate is a pointer for functions/methods...
below is my code but when i run it, nothing is being shown... what could be the problems?
public class ClassHandler
{
public delegate void DoProcesses();
public event DoProcesses DoProcessesEvent;
}
public class Class1
{
public void Func1()
{
Console.WriteLine("Class 1 doing function 1");
}
public void Func2()
{
Console.WriteLine("Class 1 doing function 2");
}
}
public class Class2
{
public void Func1()
{
Console.WriteLine("Class 2 doing function 1");
}
public void Func2()
{
Console.WriteLine("Class 2 doing function 2");
}
}
class Program
{
static void Main(string[] args)
{
Class1 cs1 = new Class1();
Class2 cs2 = new Class2();
ClassHandler main = new ClassHandler();
main.DoProcessesEvent += new ClassHandler.DoProcesses(cs1.Func1);
main.DoProcessesEvent += new ClassHandler.DoProcesses(cs1.Func2);
main.DoProcessesEvent += new ClassHandler.DoProcesses(cs2.Func1);
main.DoProcessesEvent += new ClassHandler.DoProcesses(cs2.Func2);
main.DoProcessesEvent += new ClassHandler.DoProcesses(ff); // this line here is causing an error: An object reference is required for the non-static field, method, or property 'TryDelegatesAndEvents.Program.ff()'
Console.Read();
}
public void ff()
{
Console.WriteLine("gggg");
}
}
UPDATE: how do i raise the event so it will execute the methods already?
Problem with this line: main.DoProcessesEvent += new ClassHandler.DoProcesses(ff)
That is because your method ff() is a non-static method and you can't access it directly like that from a static method.
Make your method ff as static, or create and object of the containing class and assign the method with an instance of it.
For Comments: The reason you are not seeing anything is because you are just binding them to an event DoProcessesEvent, but you are not raising the event any where. You are only defining the handler for the event.
EDIT:
Change your ClassHandler class to:
public class ClassHandler
{
public delegate void DoProcesses();
public event DoProcesses DoProcessesEvent;
public void OnDoProcessEvent()
{
if (DoProcessesEvent != null)
DoProcessesEvent();
}
}
In your Main method before Console.Read(); Type:
main.OnDoProcessEvent();
This will raise the event and it will handled from the application and will give you the following output.
Class 1 doing function 1
Class 1 doing function 2
Class 2 doing function 1
Class 2 doing function 2
gggg
change main.DoProcessesEvent += new ClassHandler.DoProcesses(ff); to main.DoProcessesEvent += new ClassHandler.DoProcesses(new Program().ff); or make ff static
Well it does not compile due to the line:
main.DoProcessesEvent += new ClassHandler.DoProcesses(ff);
The error VS spits out is that:
An object reference is required for the non-static field, method, or property 'ConsoleApplication2.Program.ff()'
Just change your ff() method to be static to get around it.
Eg:
public static void ff()
{
Console.WriteLine("gggg");
}
Besides the problem pointed out in earlier comments, You have to trigger the event.
make a copy of an event before you check it for null and fire it. This will eliminate a potential problem with threading where the event becomes null at the location right between where you check for null and where you fire the event:
// Copy the event delegate before checking/calling
EventHandler copy = DoProcessesEvent ;
if (copy != null)
copy(this, EventArgs.Empty); // Call any handlers on the copied list
This will ensure that your event fires and you will get the result.
Just to add to #Habib's answer, it would be fairly unusual to subscribe instance class methods as event handlers of an object potentially in another scope (e.g. what happens if Class1 goes out of scope, yet main() still has a subscription?). A more common scenario would be to subscribe (and de-subscribe) handlers in the same scope, often in an asynchronous manner (the below events are still raised synchronously).
namespace ConsoleApplication1
{
public delegate void ProcessCompletedEvent(string description);
public class Class1
{
public void Func1()
{
// Do Func1 work
Thread.Sleep(500);
RaiseEvent("Func1 completed");
}
public void Func2()
{
// Do Func2 work
Thread.Sleep(1000);
RaiseEvent("Func2 completed");
}
private void RaiseEvent(string description)
{
if (ProcessCompleted != null)
{
ProcessCompleted(description);
}
}
public event ProcessCompletedEvent ProcessCompleted;
}
class Program
{
static void Main(string[] args)
{
Class1 cs1 = new Class1();
// Wire up event handler
cs1.ProcessCompleted += new ProcessCompletedEvent(MyHandler);
cs1.Func1();
cs1.Func2();
Console.Read();
// Remove the subscription
cs1.ProcessCompleted -= MyHandler;
}
// *** Is in the same scope as main, which subscribes / desubscribes
public static void MyHandler(string description)
{
Console.WriteLine(description);
}
}
}