Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload for 'method' matches delegate 'System.EventHandler'

I am trying to build a program that, once the button was click, every 5 second will perform the function (OnTimed).

Below is the code so far:

private void bntCapture_Click(object sender, RoutedEventArgs e)
{ 
    DispatcherTimer t1 = new DispatcherTimer();
    t1.Interval = TimeSpan.FromMilliseconds(5000);
    t1.IsEnabled = true;
    t1.Tick += new EventHandler(OnTimed);
    t1.Start();
}

void OnTimed(object sender, ElapsedEventArgs e)
{

    imgCapture.Source = imgVideo.Source;
    System.Threading.Thread.Sleep(1000);
    Helper.SaveImageCapture((BitmapSource)imgCapture.Source);
} 

When i run the code, it show the error:

"No overload for 'method' matches delegate 'System.EventHandler'

like image 707
KayX Avatar asked Nov 09 '11 15:11

KayX


4 Answers

The signature of the event-handler method isn't compatible with the delegate type.

Subsribers to the DispatcherTimer.Tick event must be of the EventHandler delegate type, which is declared as:

public delegate void EventHandler(object sender, EventArgs e);

Try this instead:

void OnTimed(object sender, EventArgs e)
{
   ...
}
like image 81
Ani Avatar answered Sep 23 '22 11:09

Ani


If you using Windows phone 8.1 then you need the following

private void OnTimed(object sender, object e) {
      // You Code Here
 }
like image 31
Otto Kanellis Avatar answered Sep 26 '22 11:09

Otto Kanellis


Method OnTimed has to declared like this:

 private void OnTimed(object sender, EventArgs e)
 {
     // Do something
 }
like image 26
Fischermaen Avatar answered Sep 26 '22 11:09

Fischermaen


I know it might be a little bit late, but i just wanted to throw in a bit more for anyone with this problem:

timer.Tick += new EventHandler(Method);

public void Method(object sender, EventArgs e)
{
//Do Something
}

solves the Problem.

It can also be written like this: timer.Tick += Method;

timer.Tick += Method;

public void Method(object sender, EventArgs e)
{
//Do Something
}

Hope it helps!

like image 44
B Wyss Avatar answered Sep 23 '22 11:09

B Wyss