7

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'

abatishchev
  • 98,240
  • 88
  • 296
  • 433
KX.T
  • 164
  • 2
  • 3
  • 7

6 Answers6

13

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)
{
   ...
}
Ani
  • 111,048
  • 26
  • 262
  • 307
2

If you using Windows phone 8.1 then you need the following

private void OnTimed(object sender, object e) {
      // You Code Here
 }
Otto Kanellis
  • 3,629
  • 1
  • 23
  • 24
1

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!

B Wyss
  • 45
  • 1
  • 8
1

Method OnTimed has to declared like this:

 private void OnTimed(object sender, EventArgs e)
 {
     // Do something
 }
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
0

In my case I was using a custom winforms control for digitalPersona fingerprint recognition.

When I tried to overload the 'OnComplete' method it would come up with 'No overload...'

private void FingerprintVerificationControl_OnComplete(object control, DPFP.FeatureSet featureSet, DPFP.Gui.EventHandlerStatus eventHandlerStatus)

This is what it looked like.

I looked into the assembly and I noticed that third parameter had the 'ref' keyword attached to it. I added it in my code and it worked:

private void FingerprintVerificationControl_OnComplete(object control, DPFP.FeatureSet featureSet, ref DPFP.Gui.EventHandlerStatus eventHandlerStatus)
Cristian G
  • 460
  • 1
  • 4
  • 22
  • Unfortunatly, this has nothing to do with the original question (which has already been sufficiently answered). –  Oct 11 '17 at 11:21
  • Ye but I googled the title of this post and I found the answer being a different one than the one selected here. Can it be that hard for someone else to encounter the same problem like I did? I didn't post it for rep or w/e - I want to help others, that's the whole point for this site.. – Cristian G Oct 12 '17 at 19:33
  • The correct course of action would be to create your own question fitting your problem, complete with the right tags (Windows Forms, etc. ..) and answer it yourself. –  Oct 12 '17 at 19:41
0

Dispatcher.Tick is simple EventHandler:

public event EventHandler Tick;

So EventHandler parameters should be:

void OnTimed(object sender, EventArgs e)

Not the

void OnTimed(object sender, ElapsedEventArgs e)

Looks like you a bit overlooked around the System.Timers.Timer.Elapsed event which is:

public event ElapsedEventHandler Elapsed

public delegate void ElapsedEventHandler(
    Object sender,
    ElapsedEventArgs e
)
sll
  • 61,540
  • 22
  • 104
  • 156