简体   繁体   中英

Custom event handler is repeating itself

My custom event handler that executes whenever the login is complete is repeating itself.

By repeating itself, I mean that at the first time the event happens the handler executes only once. BUT, when the event happens a second time, the handler executes 2 times! And when the event happens the third time, the handler executes 3 times, and so on.

Here's my code. Anything I overlooked?

Subscription to the event

SignIn signIn = new SignIn();
signIn.Login_Complete += new SignIn.EventHandler(recieveLoginResult);
pb.IsVisible = true;
signIn.Login(control.username.Text, control.password.Password);

The event

class SignIn
{
    public event EventHandler Login_Complete;
    public event EventHandler Logout_Complete;
    public event EventHandler Signup_Complete;
    public delegate void EventHandler(Object sender, String message);

...

    public void Login(String username, String password)
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("user", username);
        parameters.Add("pass", password);

        PostClient client = new PostClient(parameters);
        client.DownloadStringCompleted += (senders, ex) =>
            {
                if (ex.Error == null)
                {
                    //Process the result...
                    Login_Complete(this, ex.Result);
                }
                else
                {
                    Login_Complete(this, "An error occurred. The details of the error: " + ex.Error);
                }
            };
        client.DownloadStringAsync(new Uri("(SOME URL HERE)", UriKind.Absolute));
    }

The event handler

    private void recieveLoginResult(object sender, String loginResult)
    {
        SignIn signin = new SignIn();
        signin.Login_Complete -= recieveLoginResult;

        //Check if the result is numeric or not.
        bool textIsNumeric = true;
        try
        {
            int.Parse(loginResult);
        }
        catch
        {
            textIsNumeric = false;
        }

        if (textIsNumeric == true)
        {
            //Logged in successfully.
            popup.IsOpen = false;
            loginName.Text = control.username.Text;
            sessionID = int.Parse(loginResult);
        }
        else
        {
            //Did not log in successfully.
            MessageBox.Show(loginResult, "Error", MessageBoxButton.OK);
        }

        pb.IsVisible = false;
    }

Unsubscribe before subscribing.

SignIn signIn = new SignIn();
signIn.Login_Complete -= recieveLoginResult;
signIn.Login_Complete += recieveLoginResult;
pb.IsVisible = true;
signIn.Login(control.username.Text, control.password.Password);

It won't do anything if it's not already subscribed, but if it is it will ensure that you're not subscribing twice. As for your other problem, you might be able to solve it by just deleting the other line of code that unsubscribes after you do the above.

Make sure that you are removing the event handler from the same object that you are subscribing to. It looks like you are creating new object to un-subscribe from that event.

SignIn signIn = new SignIn();
 signIn.Login_Complete += new SignIn.EventHandler(recieveLoginResult);


 SignIn signin = new SignIn(); // this could be the problem...***
    signin.Login_Complete -= recieveLoginResult;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM