Reputation: 299
I have a problem getting out of a method after the time runs out. I don't want to exit the whole program, but only this function during its execution. I need to get out of the method Check, not only the TimerTick.
I tried it with return;
, Enviroment.Exit(0);
that one closes everything, and with goto
but I can't go to an identifier outside the void TimerTick like:
void TimerTick(Object obj, ElapsedEventArgs e)
{
Console.WriteLine("Max time reached");
goto Finished;
}
// some code
Finished:
Console.WriteLine("Stopped");
// end of the Void Check
However this is the current code what does not work like I want.
public void Check()
{
// 4 seconds
Timer TimerOne = new Timer(4 * 1000);
void TimerTick(Object obj, ElapsedEventArgs e)
{
Console.WriteLine("Max time reached");
TimerOne.Stop();
// goto???
}
TimerOne.Elapsed += TimerTick;
TimerOne.Start();
// This for loop will go as long all items inside the list are entered
for (int i = 0; i < 99; i++)
{
if (ListOne.Test.Contains(UserInput()))
{
Console.WriteLine("Correct!");
points++;
if (points == ListOne.Test.Capacity)
break;
else
continue;
}
else
Console.WriteLine("Wrong!");
}
TimerOne.Stop();
Console.WriteLine("Stopped\n");
}
I hope it's clear to understand what I mean, I tried to sum everything up.
Upvotes: 0
Views: 216
Reputation: 117029
If you want to stick with your current code then you can do this:
public void Check()
{
// 4 seconds
Timer TimerOne = new Timer(4 * 1000);
var expired = false;
void TimerTick(Object obj, ElapsedEventArgs e)
{
Console.WriteLine("Max time reached");
TimerOne.Stop();
expired = true;
}
TimerOne.Elapsed += TimerTick;
TimerOne.Start();
// This for loop will go as long all items inside the list are entered
for (int i = 0; i < 99; i++)
{
if (expired)
{
break;
}
if (ListOne.Test.Contains(UserInput()))
{
Console.WriteLine("Correct!");
points++;
if (points == ListOne.Test.Capacity)
break;
else
continue;
}
else
Console.WriteLine("Wrong!");
}
TimerOne.Stop();
Console.WriteLine("Stopped\n");
}
Upvotes: 2
Reputation: 1689
If the intention is to allow only 4 seconds for answering the 99 questions and exit the function if it times out, you could check the time in the for-loop and break if the time has been exceeded.
This example uses a System.Diagnostics.Stopwatch
to check the elapsed time:-
public void Check()
{
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 99; i++)
{
if (sw.ElapsedMilliseconds > 4000)
{
break;
}
if (ListOne.Test.Contains(UserInput()))
{
Console.WriteLine("Correct!");
points++;
if (points == ListOne.Test.Capacity)
{
break;
}
else
{
continue;
}
}
else
{
Console.WriteLine("Wrong!");
}
}
Console.WriteLine("Stopped\n");
}
(note, you could move or add a check to after the question depending on your requirements. You could also add a flag in the for loop so you could check afterwards if all the questions were answered before it exited)
A more elegant solution might be to use a Task
with an absolute timeout:-
public void Check()
{
var task = new Task(() =>
{
for (int i = 0; i < 99; i++)
{
if (ListOne.Test.Contains(UserInput()))
{
Console.WriteLine("Correct!");
points++;
if (points == ListOne.Test.Capacity)
{
break;
}
else
{
continue;
}
}
else
{
Console.WriteLine("Wrong!");
}
}
}
// this will block for 4 seconds; if the task hasn't completed by then,
// the task function will exit, and it will run the code in the if-block below
if (!task.Wait(4000))
{
Console.WriteLine("Max time reached");
}
}
Upvotes: 0