简体   繁体   中英

Wiring and unwiring events in C# with arguments

i am trying to make use of events in C#. I am really new to events. Following is my code.

     public void GetVoltage(Object objName, Object objCcaVar)
{
  DynamicSystemVariable mysys = new DynamicSystemVariable("VTS::M9_Ch3", "AvgVoltage");
  mysys.ValueChanged += (sender, e) => mysys_ValueChanged(sender, e, ""); ;

}


void mysys_ValueChanged(object sender, EventArgs e,String name)
{       
    DynamicSystemVariable mysys = (DynamicSystemVariable)sender;
    Output.WriteLine(mysys.GetValue().ToString());
    Output.WriteLine("System Variable Changed");
    if (_unwireEvent)
        mysys.ValueChanged -= mysys_ValueChanged;
}

It gives me following error on the line where i am unwiring it. No overload for 'mysys_ValueChanged' matches delegate 'System.EventHandler'

I will appreciate if somebody can help me out.

Thanks Tom

Instead of sending name as a seperate parameter, create a new class deriving from EventArgs and add name parameter to this class as a property. Something like;

public class MyEventArgs : EventArgs
{
    public string Name {get; private set;}
    public MyEventArgs(EventArg e, string name)
    {
        this.Name = name;
    }
}

mysys.ValueChanged += mysys_ValueChanged(sender, new MyEventArgs("some name"));

Also do not forget to change the signiture of mysys_ValueChanged .

Note that this is just a workaround, and proper way is defining a new delegate which uses MyEventArgs and using a seperate handling method instead of an inline method.

The problem is that you haven't register mysys_ValueChanged . You registered an anonymous method.

You have to change your code to:

public void GetVoltage(Object objName, Object objCcaVar)
{
  DynamicSystemVariable mysys = new DynamicSystemVariable("VTS::M9_Ch3", "AvgVoltage");
  mysys.ValueChanged += this.OnEvent;

}

private void OnEvent(object sender, EventArgs e)
{
  mysys_ValueChanged(sender, e, "");
}

void mysys_ValueChanged(object sender, EventArgs e,String name)
{       
    DynamicSystemVariable mysys = (DynamicSystemVariable)sender;
    Output.WriteLine(mysys.GetValue().ToString());
    Output.WriteLine("System Variable Changed");
    if (_unwireEvent)
        mysys.ValueChanged -= OnEvent;  // Unregister what you've registered.
}

EDIT

Having new input it could be solved that way:

public void GetVoltage(Object objName, Object objCcaVar)
{
  DynamicSystemVariable mysys = new DynamicSystemVariable("VTS::M9_Ch3", "AvgVoltage");

  EventHandler<EventArgs> handler = null;
  handler = (s, e) => mysys_ValueChanged(s, e, "", handler);

  mysys.ValueChanged += handler;   
}

void mysys_ValueChanged(object sender, EventArgs e, String name, EventHandler<EventArgs> handler)
{       
    DynamicSystemVariable mysys = (DynamicSystemVariable)sender;
    Output.WriteLine(mysys.GetValue().ToString());
    Output.WriteLine("System Variable Changed");
    if (_unwireEvent)
        mysys.ValueChanged -= handler;  // Unregister what you've registered.
}

I haven't tried it but I think this could work - but in my opinion it's hacked.

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