简体   繁体   中英

Passing Event handler as parameter

Okay I don't really know what's wrong with my code and What's going on so

class Activity has the following methods

protected struct EventParams
    {
        public object sender;
        public EventArgs e;
    }
private EventParams WaitEventRaise_Body(ref EventHandler<EventArgs> handler, int timeout)
    {
        AutoResetEvent receiver = new AutoResetEvent(false);
        EventParams result = new EventParams();

        EventHandler<EventArgs> handle = new EventHandler<EventArgs>((sender, e) =>
        {
            result.e = e;
            result.sender = sender;
            receiver.Set();
        });

        handler += handle;

        if (timeout > 0)
        {
            receiver.WaitOne(timeout);
        }
        else
        {
            receiver.WaitOne();
        }

        return result;
    }

protected EventParams WaitEventRaise(ref EventHandler<EventArgs> handler)
{
    return WaitEventRaise_Body(ref handler, -1);
}
protected EventParams WaitEventRaise(ref EventHandler<EventArgs> handler, int timeout)
{
    return WaitEventRaise_Body(ref handler, timeout);
}

Okay so I find myself writing over and over again the AutoResetEvent thing so I decided to create a method. But when I try to call this method from derived class Bot : Activity

EventParams eventResult = WaitEventRaise(ref currentJob.JobReported);

gives

Error 30 The best overloaded method match for Project.Activity.WaitEventRaise(ref System.EventHandler)' has some invalid arguments

currentJob is a Job : Activity class that has the event

public event EventHandler<JobReport> JobReported;

and

class JobReport : EventArgs

What I want to do is there is a bot thing does jobs, actually it creates jobs and waits them for finish their work. Job class raises event inside to make bot class noticed that it finished its work. And bot class waits until job raises event. So I hope its clear. And I'm sorry that english is not my native language.

Basically, you can't refer to an event like that. Two options:

  • Pass in delegates for "adding a handler" and "removing a handler":

     EventParams eventResult = WaitEventRaise<JobReport>(handler => currentJob.JobReported += handler, handler => currentJob.JobReported -= handler); 

    where WaitEventRaise would be declared as:

     EventParams WaitEventRaise<T>(Action<EventHandler<T>> add, Action<EventHandler<T>> remove) where T : EventArgs 
  • Pass in the EventInfo corresponding to the event, which you'd fetch with reflection

Neither of these is terribly pleasant - it's a problem that Rx comes up against as well.

i don't know what's Job class contains or what currentJob.JobReported is, but judging from the error WaitEventRaise method needs System.EventHandler which is a method like

private void MeMethod(object sender, EventArgs e)
{
}

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