Invoking EventHandler generic, TargetParameterCountException

Posted by Am on Stack Overflow See other posts from Stack Overflow or by Am
Published on 2010-03-20T18:32:03Z Indexed on 2010/03/20 18:41 UTC
Read the original article Hit count: 571

Filed under:
|
|
|

Hi,

I have a DirectoryMonitor class which works on another thread. It has the following events declared:

public class DirectoryMonitor
{
    public event EventHandler<MonitorEventArgs> CreatedNewBook;
    public event EventHandler ScanStarted;
    ....
}

public class MonitorEventArgs : EventArgs
{
    public Book Book { get; set; }
}

There is a form using that monitor, and upon receiving the events, it should update the display.

Now, this works:

    void DirectoryMonitor_ScanStarted(object sender, EventArgs e)
    {
        if (InvokeRequired)
        {
            Invoke(new EventHandler(this.DirectoryMonitor_ScanStarted));
        }
        else {...}
    }

But this throws TargetParameterCountException:

    void DirectoryMonitor_CreatedNewBook(object sender, MonitorEventArgs e)
    {
        if (InvokeRequired)
        {
            Invoke(new EventHandler<MonitorEventArgs>(this.DirectoryMonitor_CreatedNewBook));
        }
        else {...}
    }

What am I missing?

© Stack Overflow or respective owner

Related posts about c#

Related posts about eventhandler