Answer the question
In order to leave comments, you need to log in
How to repeat the last action in the application?
That's what on tz
When the console starts, the user should be given the option to select the job to run. After completing the task, you need to ask the user if he wants to repeat the current task, or select another one, or exit the application.
Answer the question
In order to leave comments, you need to log in
Perform the action in a separate method, and then simply call this method again. More or less like this:
static Action lastAction = null;
static void AnyAction()
{
Console.WriteLine("Выполняю какое-то действие. Не отключайтесь...");
Thread.Sleep(3000);
}
static void Repeat()
{
Console.WriteLine("Хотите повторить? [Д/н]");
if (char.ToUpper(Console.ReadKey().KeyChar) == 'Д')
{
Console.WriteLine();
lastAction();
Repeat();
}
}
static void Main(string[] args)
{
lastAction = AnyAction;
lastAction();
Repeat();
}
static Queue<Action> actions = new Queue<Action>();
static void AnyAction()
{
Console.WriteLine("Выполняю какое-то действие. Не отключайтесь...");
Thread.Sleep(3000);
}
static void Main(string[] args)
{
actions.Enqueue(AnyAction);
while (actions.Count > 0)
{
actions.Dequeue()();
Console.WriteLine("Хотите повторить? [Д/н]");
if (char.ToUpper(Console.ReadKey().KeyChar) == 'Д')
{
Console.WriteLine();
actions.Enqueue(AnyAction);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question