Doubt Threads in C #

2

Good morning I am studying C # and I have found the need to make my program pause, until x seconds pass or until the same user generates an event. I tried to transfer my knowledge of java threads to C # but it does not work correctly. My question is: can you pause the program and generate an event either if the user has performed an action or when x seconds have passed? PS: I'm working on a console application in .NET PD2: thanks in advance.

    
asked by Jesus Romero 03.11.2018 в 09:43
source

1 answer

1

I recommend you use the Timer class of c #. I give you an example of how it works:

    private static System.Timers.Timer crono;

    private static void SetTimer(double tiempo)

            {
    //EL TIEMPO LO MULTIPLICO POR 1000 PARA QUE ESTE EN SEGUNDOS.

                crono = new System.Timers.Timer(tiempo*1000);
                crono.AutoReset = false;
                crono.Elapsed += OnTimedEvent;
                crono.Enabled = true;
            }

            private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
            {
//AQUI CODIFICAS EL EVENTO QUE QUIERES QUE SE EJECUTE CUANDO PASEN LOS SEGUNDOS
            }
        }

To start the timer, call SetTimer(tiempo); in your program

And to stop it

crono.Stop();
crono.Dispose();
    
answered by 05.11.2018 / 10:34
source