C#: keep the timer running even after the end of the method

Any idea how can I keep the timer running even after the method ends

public static void Method(){
    Timer Timer = new Timer(state: null, dueTime: 0, period: 60000, callback: (o) => {
        Console.WriteLine("Thank you <3");
    });
}

The code above displays something like this:

1. Thank you <3

I want to achieve something like this:

1. Thank you <3 (after 0 min)
2. Thank you <3 (after 1 min)
3. Thank you <3 (after 2 min)
4. ...

I tried GC.KeepAlive(Timer); but doesn't seem to work.

I know this is related to the garbage collector, and I can work around this and create Timer as a global variable, but are there any better suggestions?



from Recent Questions - Stack Overflow https://ift.tt/3kXZvnZ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations