-2

I have started a new job, where the last dev left they want a program he started to be finished .

I have got to this problem and have looked at it for half a day.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    logTimer = new System.Windows.Threading.DispatcherTimer();
    logTimer.Tick += new EventHandler(logTimer_Tick);
    logTimer.Interval = new TimeSpan(0, 0, 0, 1);
    logTimer.Start();

    txtLogData.Text = Logger.GetLines();

    try
    {
        DataProcessor gaugeProcessor = new DataProcessor(SQLConnectionString);
        gaugeProcessors.Add(gaugeProcessor);

        grdProcessor.ItemsSource = gaugeProcessors;

        List<GaugePort> ports = SQLClient.GetGaugePorts(SQLConnectionString);

        foreach(GaugePort port in ports)
        {
            GaugePortListener newListener = new GaugePortListener(port);
            listeners.Add(newListener);
        }

        grdPorts.ItemsSource = listeners;
    }
    catch(Exception ex)
    {

    }
}

I am getting an error on line 4 "No Overload for ' logTimer_Tick' matches delegates 'Event Handler'"

The Function it calls dose exist and looks like this

private void logTimer_Tick(object sender, EventArgs e)
{
     txtLogData.Text = Logger.GetLines();
}

I have had a look at the links below but i have drawn a blank http://www.yoda.arachsys.com/csharp/threads/parameters.shtml

C# method name expected

Any ideas would be great

Thanks in advance

EDIT

Change the wording for the error message "Typo"

Community
  • 1
  • 1
  • Does it help if you just change it to the newer form `logTimer.Tick += logTimer_Tick;`? – Sami Kuhmonen Apr 21 '17 at 10:39
  • 2
    Something odd going on. Your event handler matches the delegate https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.tick(v=vs.110).aspx – Jamiec Apr 21 '17 at 10:40
  • 1
    I've just tested this code in LinqPad and it compiles fine. I'm not sure what's going on, can you post a [MVCE](https://stackoverflow.com/help/mcve) ? – DaveShaw Apr 21 '17 at 10:42
  • @SamiKuhmonen That fixed it Thank you for the help and quick response – Briggers123 Apr 21 '17 at 10:57
  • 1
    That is strange... I cannot find any evidence why that change alone would be enough. For example, [this](http://stackoverflow.com/questions/2749868/new-eventhandlermethod-vs-method) and [this](http://stackoverflow.com/questions/550703/c-difference-between-anevent-and-new-eventhandleranevent) seem to suggest they will both produce the same results and should work – musefan Apr 21 '17 at 11:04
  • 1
    What platform are you using? WPF, WinForms, Windows Phone, ... ? And are you sure your exception is with *matches delegates* instead of *matches delegate*? – MetaColon Apr 21 '17 at 11:19

1 Answers1

0

Directly use the method:

logTimer.Tick += logTimer_Tick;

This should help, as the compiler does strange things with your EventHandler.

The weird thing is that your code seems to work on my machine - that means the code you posted isn't equal to the code you tried or it's a bug caused by your compiler. Or, as a third possibility, the logtimer isn't a WinForms timer, then I can't reproduce your problem.

In this third case it may be possible that the second parameter isn't an EventArg (even though it'd be strange that it works if you don't use the EventHandler stuff). Then you could try an object as second parameter:

private void logTimer_Tick (object sender, object e)

It seems to be neccessary for Windows phone 8.1 (No overload for 'method' matches delegate 'System.EventHandler').

Community
  • 1
  • 1
MetaColon
  • 2,895
  • 3
  • 16
  • 38
  • 2
    What strange things does it do? Do you have any references? I am interested in learning more about this – musefan Apr 21 '17 at 11:04