A
A
akass2015-07-08 02:16:28
WPF
akass, 2015-07-08 02:16:28

How to stop the program from stopping?

There is a timer, every 5 minutes which methods are executed in timer.elapsed, only because there is nothing else, the timer is created and the program ends before reaching 5 minutes.
So far, I have made an infinite counter in while (true), how should I solve such a problem correctly?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
Larry Underwood, 2015-07-08
@Hydro

var timer = new Timer((e) =>  {  });
timer.Change(0, 1000);
Console.ReadKey();

:)

V
Vitaly Pukhov, 2015-07-08
@Neuroware

It depends on what you need and how reliable it should be. In the simplest case, it is better to separate the program that executes the methods and the program that executes the timer so that the timer is not covered in case of an execution. You can also use while (true), just hang Thread.Sleep (five minutes) in it for the waiting time

S
Stanislav Silin, 2015-07-08
@byme

It all depends on your architecture, if this is a simple console application and you don’t need anything other than a timer, then I would do this:

Timer timer = new Timer((obj) => 
{
      Console.WriteLine("Hello");
});
timer.Change(0, 1000);
Thread.CurrentThread.Suspend();

The thread stops, nothing crashes, and the timer works as it should.

B
Boris the Animal, 2015-07-08
@Casper-SC

Usually, when they say that something does not work, they attach the code. I sat and thought, and what does WPF have to do with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question