0

c# Timer (System.Timers.Timer) is used to periodically trigger an event within windows form application. I would like to call function (for example logger() function) within the even handler. logger() is not a static method.

The function assigned to ElapsedEventHandler is a static function and therefore cannot call non-static methods. Code example:

public partial class MainForm : Form {
    //...
    private MyClass myClass;
    //...
} 

private void SomeButton_Click(object sender, EventArgs e) {
    //...
    System.Timers.Timer t = new System.Timers.Timer(5000); 
    t.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
    t.Enabled = true;
    //...
}

static void OnTimerElapsed(object sender, ElapsedEventArgs e) {
    //...
    // here call myClass.doSomething();
    //...
}

How would be the correct way to go about this task? I do know that static variables/methods are not possible to be used within the OnTimerElapsed() - that is clear. I mainly ask to check whether there is another way of calling OnTimerElapsed(), maybe a non-static method or another timer type or handler method? Or if there is a way to pass the instance of myClass to the OnTimerElapsed()

Edit: it would be preferable to keep the myClass non-static, that is why this question.

Miroslav Radojević
  • 487
  • 1
  • 5
  • 20
  • 2
    Of course it can, why would it not be able to? Did you try it? – DavidG Jun 18 '18 at 11:03
  • It's not clear exactly how your code is structured - the fact you have `var ... ` indicates that perhaps your "non-static variable" is actually a local variable inside a function? In which case, how would you update that from _any_ other function, static or not? Could you please post a [mcve] of what you're trying to achieve. – James Thorpe Jun 18 '18 at 11:08
  • 1
    I tried to rephrase the question and modified the code. In principle I am asking about the general recommended way to implement this (I know that non-static elements are restricted in static methods) – Miroslav Radojević Jun 18 '18 at 11:40
  • There is no requirement at all to make the Elapsed event handler *static*. Just remove it to get ahead. Do beware that this is pretty dangerous in general, the timer keeps ticking even though the user closed the window, an ObjectDisposedException is pretty likely and you can't rely on the form's Begin/Invoke method to marshal the call from the worker thread. Much, much less trouble from the Timer component in the toolbox. – Hans Passant Jun 18 '18 at 12:34
  • the community for c sharp seems like an empty room.. – Async- Aug 02 '21 at 18:14

1 Answers1

2

you cannot access non-static i.e. instance field with in static function , that is reason its not working.

If you want to access instance field with in static function then you need instance of object and then you can access that instance field. Or make field static then you can access field

if you make

static var someNonStaticVariable = 1000; // for example

it will work but then you need locking around that variable or Interlocked (userful if you just want to perform increment/decrement or excahnge i.e. for numeric operation).

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263