2

I am trying to initialize a timer that calls a method every second. I need to initialize it from within another event handler.

I wrote this code but it still gives me a compile error. They, both the initialization and the event handler are in the same class.

private void Controllel_Opened(object sender, EventArgs e)
    {   
        System.Timers.Timer myTimer = new System.Timers.Timer();
        myTimer.Elapsed += new System.Timers.ElapsedEventHandler(DisplayTimeEvent);
        myTimer.Interval = 1000; // 1000 ms is one second
        myTimer.Start();

    }


    public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
    {
        // code here will run every second
    }

Any suggestions?

ib11
  • 2,530
  • 3
  • 22
  • 55
  • 1
    It gives you a compile error.. okay, which one? – Rob Apr 22 '16 at 04:49
  • @Rob -- No overload for 'DisplayTimeEvent' matches delegate 'System.Timers.ElapsedEventHandler' – ib11 Apr 22 '16 at 04:50
  • 1
    It's likely you're referencing the wrong dll. However, you can simplify your code: `myTimer.Elapsed += DisplayTimeEvent;` which may fix your issue. – Rob Apr 22 '16 at 04:51
  • @Ron -- Thanks for your question, it helped. When I looked, I noted that I am missing the reference from the `ElapsedEventArgs`. So you led me to the solution even if indirectly. – ib11 Apr 22 '16 at 05:00

2 Answers2

2

Well, thanks, it made me look and I found that I simply was missing the reference before the ElapsedEventArgs in the event handler.

So the code fully works as:

public static System.Timers.Timer myTimer = new System.Timers.Timer();    

private void Controllel_Opened(object sender, EventArgs e)
{   
    myTimer.Elapsed += new System.Timers.ElapsedEventHandler(DisplayTimeEvent);
    myTimer.Interval = 1000; // 1000 ms is one second
    myTimer.Start();
}

public static void DisplayTimeEvent(object source, System.Timers.ElapsedEventArgs e)
{
    // code here will run every second
}

Thanks for looking.

ib11
  • 2,530
  • 3
  • 22
  • 55
  • This looks like a concurrency nightmare waiting to happen. What sort of code do you plan on putting in DisplayTimeEvent? – Robert Harvey Apr 22 '16 at 05:03
  • @Robert Harvey ♦ -- I am trying to send a method to a form to hide. – ib11 Apr 22 '16 at 05:11
  • @Robert Harvey ♦ -- OK, here it the question of the exact issue I am trying to solve. The timer works, all the other methods and the form works. I only need to make the call from the timer event handler cross-thread safe. [see http://stackoverflow.com/questions/36785638/making-a-cross-thread-call-to-hide-a-form-visual-c-sharp] (http://stackoverflow.com/questions/36785638/making-a-cross-thread-call-to-hide-a-form-visual-c-sharp) – ib11 Apr 22 '16 at 05:37
-1

You can do the same from client side also.

[WebMethod]
public static void  MyServerMethod()
{
   //Code goes herer.
}

setInterval(function () {
              $.post("../MyPage.aspx", function (data) {

            $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: '../MyPage.aspx/MyServerMethod',
            data: '{}',
            async: false,
            success: function (response) {
            
              // code goes here. 
           
                });
            }, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Shahbaz Ahmad
  • 161
  • 1
  • 9