0

I have event handlers for intercepting call logs on windows mobile. The problem is more specific to event handlers in C# rather than windows mobile. I am able to attach and detach event handlers for the first time. The proble is: I am not able to attach event handler after its detached at least once. Why are the event handlers not invoked after its detached and again attached?

Below is my code:

private static SystemState stateIncoming = null;
private static SystemState stateOutgoing = null;
private static SystemState stateTalking = null;


public static void StartCallLogInterception()

{

    if (stateIncoming == null && stateOutgoing == null && stateTalking == null)
    {
        stateIncoming = new SystemState(SystemProperty.PhoneIncomingCall);
        stateIncoming.Changed += new ChangeEventHandler(stateIncoming_Changed);

        stateOutgoing = new SystemState(SystemProperty.PhoneCallCalling);
        stateOutgoing.Changed += new ChangeEventHandler(stateOutgoing_Changed);

        stateTalking = new SystemState(SystemProperty.PhoneCallTalking);
        stateTalking.Changed += new ChangeEventHandler(stateTalking_Changed);
    }
}

public static void EndCallLogInterception()

{

    if (stateIncoming != null && stateOutgoing != null && stateTalking != null)
    {
        stateIncoming.Changed -= new ChangeEventHandler(stateIncoming_Changed);
        stateIncoming = null;

        stateOutgoing.Changed -= new ChangeEventHandler(stateOutgoing_Changed);
        stateOutgoing = null;

        stateTalking.Changed -= new ChangeEventHandler(stateTalking_Changed);
        stateTalking = null;
    }
}

EDIT: I updated code to include class level variable. Also, below answers conflict with each other. If I am disposing object, I must re-create the object when I need to attach event handler. Does this make sense?

EDIT 2: The problem is not with objects or event handling code. I am using LargeIntervalTimer from OpenNETCF. Whenever I am running timer using LargeIntervalTimer, the event handler is not attached properly. Without LargeIntervalTimer, everything is working fine.

Let me Ask
  • 1,029
  • 1
  • 8
  • 33
  • 2
    I suspect that your problem is something else. There's no limitation in C# to how many times you can attach and detach event handlers. Maybe post some more code and what the exact issues you're having are... – BFree Nov 26 '10 at 14:21

3 Answers3

0

Well, it's not really clear from just the code you've given, but I wonder whether it's because you're never disposing of the SystemState objects you're creating. If you change your code to dispose of them properly when you unsubscribe, that may help.

Alternatively, don't bother keeping on creating new objects - just create the three objects up-front, and then subscribe/unsubscribe as appropriate.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

You don't need the

stateTalking.Changed -= new ChangeEventHandler(stateTalking_Changed);

code. First, you are not removing the same thing you put in, you are removing a new instance og the ChangeVenetHandler. Second, all event handlers are removed when you run

stateTalking = null;

because of the Garage Collection.

As Jon Skeet said, you never run the code

stateTalking.Dispose();

before you remove it.

Jan Sverre
  • 4,617
  • 1
  • 22
  • 28
0

Check your if statements. Place a break point and make sure that you even enter the condition that wires-up the handlers. I suspect that you are not reaching the code in subsequent calls, likely because one of the objects is not null.

Les
  • 10,335
  • 4
  • 40
  • 60