For Sample ....
SampleClass :
public class SampleClass
{
public delegate void BeforeEditorHandle();
public event BeforeEditorHandle OnBeforeEditor;
}
MainMethod
static void Main(string[] args)
{
SampleClass sc = new SampleClass();
// Add Event
sc.OnBeforeEditor +=new SampleClass.BeforeEditorHandle(sc_OnBeforeEditor);
// Remove Event
sc.OnBeforeEditor -= new SampleClass.BeforeEditorHandle(sc_OnBeforeEditor);
}
And , if I add the event by dynamic like this ...↓
sc.OnBeforeEditor += () => { };
Should I remove the event like ↓
sc.OnBeforeEditor -= () => { };
But I think this is very ugly when I have too much sources in the event....
Can anybody tell me the best way to remove the event please ?
You can assign the event handler/lambda to a variable which you can then subscribe and unsubscribe:
var myHandler = () => { };
sc.OnBeforeEditor += myHandler;
sc.OnBeforeEditor -= myHandler;
I'm pretty sure your code here won't work:
And , if I add the event by dynamic like this ...↓
sc.OnBeforeEditor += () => { };
Should I remove the event like ↓
sc.OnBeforeEditor -= () => { };
This is because restating the lambda creates a new different lambda.
You need to store the old reference and use it to unsubscribe:
BeforeEditorHandle myHandler=() => { }
sc.OnBeforeEditor += myHandler;
...
sc.OnBeforeEditor -= myHandler;
For easier unsubscribing you can collect your event handlers in a collection (For example List<BeforeEditorHandle>).
From MSDN:
It is important to notice that you
cannot easily unsubscribe from an
event if you used an anonymous
function to subscribe to it. To
unsubscribe in this scenario, it is
necessary to go back to the code where
you subscribe to the event, store the
anonymous method in a delegate
variable, and then add the delegate to
the event. In general, we recommend
that you do not use anonymous
functions to subscribe to events if
you will have to unsubscribe from the
event at some later point in your
code. For more information about
anonymous functions, see Anonymous
Functions (C# Programming Guide).
Related
In C#/Unity, is there a way to pass more arguments to an event handler that were not specified in the delegate?
Assume that I cannot modify the delegate. (It comes from an external class.)
I need to be able to do this so I can unsubscribe from my handler later (onClick -= HandleClick somewhere else in the code).
Example:
delegate void A(str s);
event A onClick;
Awake() {
onClick += HandleClick;
}
void HandleOnClick(str s, int a) {
// How to get access to this second argument a here?
}
I know I can do the following:
int a = 10;
onClick += (s) => HandleOnClick(s, 10);
But this won't let me unregister the lambda (since it's an anonymous delegate) if I wire it up this way.
One way you can try to use a variable to hold the delegate reference which is able to subscribe or unsubscribe from the event.
Awake() {
int a = 10;
var e1 = (s) => HandleOnClick(s, a);
onClick += e1;
onClick -= e1;
}
I'm a beginner in unit testing and I've been searching the net for couple of hours now and I still can't find the answer to my simple question.
I have a class with the following event:
event Action<ITagData> OnTagHandled
Now I want to write a unit test to assert if the event has been raised but when I write something like:
Assert.Raises<EventArgs>(handler => m_rssiHander.OnTagHandled += handler,
handler => m_rssiHander.OnTagHandled -= handler, () => { });
I get an error like:
Cannot implicitly convert type System.EventHandler to System.Action
Can someone tell me how I can assert an event of type Action<T>?
It is because handler is type of EventHandler<EventArgs>
so m_rssiHander.OnTagHandled += handler will not work
you will have to change:
event Action<ITagData> OnTagHandled
TO
event Action<EventArgs> OnTagHandled for it to work
or any child class of EventArgs and inherit ITagData interface
e.g.
class TagDataEventArgs: EventArgs, ITagData {}
and use it as:
event Action<TagDataEventArgs> OnTagHandled
AND assert:
Assert.Raises<TagDataEventArgs>(handler => m_rssiHander.OnTagHandled += handler,
handler => m_rssiHander.OnTagHandled -= handler, () => { });
Finally I decided to do it like this:
bool wasEventRaised = false;
m_rssiHandler.OnTagHandled += data => { wasEventRaised = true;};
// Act
m_rssiHandler.ProcessTag(m_tag);
// Assert
Assert.True(wasEventRaised);
I am writing integration tests that involve FileSystemWatcher objects. To make things easier, I want to unsubscribe everything from an event delegate without having to hunt down every subscription. I already saw related post, Is it necessary to unsubscribe from events?. This is somewhat a duplicate, but I am specifically asking why this doesn't work with a FileSystemWatcher object.
It would be nice to do something like the following:
private void MethodName()
{
var watcher = new FileSystemWatcher(#"C:\Temp");
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Changed = null; // A simple solution that smells of C++.
// A very C#-ish solution:
foreach (FileSystemEventHandler eventDelegate in
watcher.Changed.GetInvocationList())
watcher.Changed -= eventDelegate;
}
No matter how the Changed event is referenced, the compiler reports:
The event 'System.IO.FileSystemWatcher.Changed' can only appear on the left hand side of += or -=
The above code works just fine, when working with an event in the same class:
public event FileSystemEventHandler MyFileSystemEvent;
private void MethodName()
{
MyFileSystemEvent += new FileSystemEventHandler(watcher_Changed);
MyFileSystemEvent = null; // This works.
// This works, too.
foreach (FileSystemEventHandler eventDelegate in
MyFileSystemEvent.GetInvocationList())
watcher.Changed -= eventDelegate;
}
So, what am I missing? It seems that I should be able to do the same with the FileSystemWatcher events.
When you declare event in your class, it is an equivalent (almost) of the following code:
private FileSystemEventHandler _eventBackingField;
public event FileSystemEventHandler MyFileSystemEvent
{
add
{
_eventBackingField =
(FileSystemEventHandler)Delegate.Combine(_eventBackingField, value);
}
remove
{
_eventBackingField =
(FileSystemEventHandler)Delegate.Remove(_eventBackingField, value);
}
}
Notice that there is no set or get accessor for event (like for properties) and you can't explicitly write them.
When you write MyFileSystemEvent = null in your class, it is actually doing _eventBackingField = null, but outside your class there is no way to directly set this variable, you have only event add & remove accessors.
This might be a confusing behavior, because inside your class you can reference an event handler delegate by event name, and can't do that outside the class.
Short answer is += and -= are public operators while = is a private operator to the class that's declaring the event.
I have the following code where SprintServiceClient is a reference to a WCF Service-
public class OnlineService
{
private SprintServiceClient _client;
public OnlineService()
{
_client = new SprintServiceClient();
}
public void AddMemberToTeam(MemberModel user, int projectId, Action<int> callback)
{
_client.AddMemberToTeamCompleted += (s, e) => callback(e.Result);
_client.AddMemberToTeamAsync(user.ToUser(), projectId);
}
}
the problem is that every time AddMemberToTeam is called it adds another callback to client.AddMemberToTeamCompleted
i.e the first time AddMemberToTeam is called the callback is called once, the second time AddMemberToTeam is called the callback is called twice ect.
Is there any way to remove the eventhandler from AddMemberToTeamCompleted once the eventhandler has been called or use another method which takes in the callback?
You can refer to your anonymous method from inside itself as long as you assign a delegate to a variable first:
EventHandler<SomeEventArgs> handler = null;
handler = (s, e) =>
{
_client.AddMemberToTeamCompleted -= handler;
callback(e.Result);
};
_client.AddMemberToTeamCompleted += handler;
Note that you need to declare the variable and assign it separately or the compiler will deem it uninitialized when you come to use it inside the method body.
The trick to making a self-unsubscribing event-handler is to capture the handler itself so you can use it in a -=. There is a problem of declaration and definite assignment, though; so we can't do something like:
EventHandler handler = (s, e) => {
callback(e.Result);
_client.AddMemberToTeamCompleted -= handler; // <===== not yet defined
};
So instead we initialize to null first, so the declaration is before the usage, and it has a known value (null) before first used:
EventHandler handler = null;
handler = (s, e) => {
callback(e.Result);
_client.AddMemberToTeamCompleted -= handler;
};
_client.AddMemberToTeamCompleted += handler;
No there is no way,
Apparantly Tim and Marc have another nice solution
But you can always just name them, and do the -= on the named eventhandler on this method ;)
Guessing your event:
_client.AddMemberToTeamCompleted += OnAddMemberToTeamCompleted;
and
public void OnAddMemberToTeamCompleted(object sender, EventArgs args)
{
_client.AddMemberToTeamCompleted -= OnAddMemberToTeamCompleted;
callback(e.Result)
}
Next problem is getting this callback in your listener. Perhaps putting it on a Property in the EventArgs (but that feels kinda dirty, I agree)
I'm using global variable named "client"
For example
client.getPagesCompleted += (s, ee) =>
{
pages = ee.Result;
BuildPages(tvPages.Items, 0);
wait.Close();
};
client.getPagesAsync(cat.MainCategoryID);
I need to clear handlers for getPagesCompleted and set another handler. How to easy clear handles?
I know client.getPagesCompleted-=new EventHandler(...). But it is very difficult. I need easy way.
I'm using client.getPagesCompleted=null but error shown. "only use += / -+"
The only way to remove an event handler is to use the -= construct with the same handler as you added via +=.
If you need to add and remove the handler then you need to code it in a named method rather using an anonymous method/delegate.
You don't have to put your event handler in a separate method; you can still use your lambda function, but you need to assign it to a delegate variable. Something like:
MyEventHandler handler = (s, ee) =>
{
pages = ee.Result;
BuildPages(tvPages.Items, 0);
wait.Close();
};
client.getPagesCompleted += handler; // Add event handler
// ...
client.getPagesCompleted -= handler; // Remove event handler
Save the event object to a variable, and use -= to unsubscribe.