N
N
nordwind20132018-07-02 11:03:22
Command line
nordwind2013, 2018-07-02 11:03:22

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.

I can't figure out how to repeat the previous action if the user chose to repeat. Everything must be done without hardcode.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2018-07-02
@nordwind2013

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();
}

Or use queues and add an action to the queue if necessary to retry:
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 question

Ask a Question

731 491 924 answers to any question