-1

This is a part of my code:

  if (direccion.Equals("este"))
        {
        System.Timers.Timer trampa = new System.Timers.Timer();
        trampa.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        trampa.Interval = 2000;
        trampa.Enabled = true;
        Console.WriteLine("Te encuentras con una granja abandonada, al entrar a la misma, te das cuenta que hay un cofre al final del pasillo principal.");
        Console.WriteLine("Al acercarte al pasillo, sientes que el piso se mueve.");
        Console.ReadLine();
        }
    }

Problem being that OnTimedEvent never gets executed, I think the timer is not working or at least not properly.

private static void OnTimedEvent(object sender, ElapsedEventArgs e)
{
    Random frn = new Random();
    int ran = frn.Next(1, 4);
    System.Timers.Timer caida = new System.Timers.Timer();
    caida.Elapsed += new ElapsedEventHandler(OnTimedEvent2);
    var direccion = Console.ReadLine();
    Console.WriteLine("¡La madera del piso esta extremadamente podrida y se despedaza hacia un abismo!");
    switch (ran)
    {
        case 1:
            caida.Interval = 2500;
            caida.Enabled = true;
            Console.WriteLine("¡Salta hacia ADELANTE para evitar caerte!");
            if (direccion.Equals("adelante"))
            {
                Console.WriteLine("Saltaste hacia adelante, sin embargo, estas atrapado entre el abismo y el cofre.");
                Console.WriteLine("Despues de esperar varios dias por rescate, mueres por deshidratacion.");
                Console.ReadLine();
                caida.Enabled = false;
            }
            else
            {
                Console.WriteLine("No saltaste donde debias y caiste al abismo.");
                Console.ReadLine();
            }
            break;

        case 2:
            caida.Interval = 2000;
            caida.Enabled = true;
            Console.WriteLine("¡Salta hacia ATRAS para evitar caerte!");
            if (direccion.Equals("atras"))
            {
                Console.WriteLine("Saltaste hacia atrás, evitando asi caerte, sin embargo ahora no puedes alcanzar el cofre.");
                Console.WriteLine("Vuelves a casa con las manos vacias.");
                Console.ReadLine();
                caida.Enabled = false;
            }
            else
            {
                Console.WriteLine("No saltaste donde debias y caiste al abismo.");
                Console.ReadLine();
            }
            break;

        case 3:
            caida.Interval = 2000;
            caida.Enabled = true;
            Console.WriteLine("¡Salta hacia IZQUIERDA para evitar caerte!");
            if (direccion.Equals("izquierda"))
            {
                Console.WriteLine("Saltas hacia la izquierda, sin embargo, ahora no puedes alcanzar el cofre.");
                Console.WriteLine("Bordeas el agujero del abismo y luego te devuelves a casa con las manos vacias.");
                Console.ReadLine();
                caida.Enabled = false;
            }
            else
            {
                Console.WriteLine("No saltaste donde debias y caiste al abismo.");
                Console.ReadLine();
            }
            break;
    }
}

private static void OnTimedEvent2(object sender, ElapsedEventArgs e)
{
    Console.WriteLine("X Y Z");
}
Emond
  • 50,210
  • 11
  • 84
  • 115
Jorge Gill
  • 175
  • 1
  • 2
  • 10
  • 2
    Is the `Console.WriteLine("Te encuentras con una granja abandonada, al entrar a la misma, te das cuenta que hay un cofre al final del pasillo principal.");` being written to your console? My guess is `if (direccion.Equals("este"))` is false. – Blue Jan 17 '18 at 16:10
  • Perhaps you need to use a case-insensitive comparison? `if (direccion.Equals("este", StringComparison.OrdinalIgnoreCase))`. Your code works for me if I set `var direccion = "este";` first. – Rufus L Jan 17 '18 at 16:16
  • What's inside `OnTimedEvent`? How do you determine *"OnTimedEvent never gets executed"*? – Sinatr Jan 17 '18 at 16:21
  • @FrankerZ It is being written, yes! – Jorge Gill Jan 17 '18 at 16:35
  • @RufusL Didn't work for me. – Jorge Gill Jan 17 '18 at 16:36
  • @Sinatr A Console.WriteLine that never gets written. – Jorge Gill Jan 17 '18 at 16:36
  • @JorgeGill Post your `OnTimedEvent` handler. – Blue Jan 17 '18 at 16:39
  • @FrankerZ There! – Jorge Gill Jan 17 '18 at 16:45
  • As FrankerZ said, your timer event is firing as requested, but each execution of the OnTimedEvent() method gets blocked at Console.ReadLine(), waiting for the user to press enter – Carvo Loco Jan 17 '18 at 17:12
  • side note: the random will work better if you instantiate the Random object once and re-use it. – Emond Jan 17 '18 at 17:19
  • Welcome to Stack Overflow! If one of the answers below answered your question, the way this site works works, you'd "accept" the answer, more here: ***[What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers)***. But only if your question really has been answered. If not, consider adding more details to the question. – Blue Jan 17 '18 at 17:33

1 Answers1

2

The problem is Console.ReadLine() in your OnTimedEvent() method. This method will run in a different thread, and your main thread is currently already listening for console input.

I recommend reading into this question/answer, for ways to read input from a different thread/adding timeouts.

Blue
  • 22,608
  • 7
  • 62
  • 92