2015-10-15 19 views
6

Sonsuz bir işlem gerçekleştirecek bir C# konsol uygulaması oluşturuyorum. Kullanıcı çıkış tuşunu bastığında uygulamayı "duraklat" durumuna nasıl getirebilirim?Kullanıcı, bir komut uygulamasının kaçışına basıldığında nasıl "Duraklatır"?

Kullanıcı çıkış tuşuna bastığında, uygulamadan çıkma ya da döngüyü kaldığı yerden devam etme seçeneğine sahip olmak istiyorum. Süreçte süreksizlik istemiyorum. kullanıcı girişi anahtarında denetlemek için herhangi bir yolu var mı

// Runs the infinite loop application 
    public static void runLoop() 
    { 
     int count = 0; 
     while (Console.ReadKey().Key!= ConsoleKey.Escape) 
     { 
       WriteToConsole("Doing stuff.... Loop#" + count.ToString()); 
       for (int step = 0; step <= int.MaxValue; step++) { 
        WriteToConsole("Performing step #" + step.ToString()); 
        if (step == int.MaxValue) 
        { 
         step = 0; // Re-set the loop counter 
        } 
       } 


       count++; 
     } 

     WriteToConsole("Do you want to exit? y/n"); 
     exitApplication(ReadFromConsole()); 
    } 

: Ben adımda 100 Esc basarsanız ben burada

101. şimdiye kadar benim yöntemdir aşamada doğru geri almak gerekir Diğer iş parçacığı, bir Esc tuşa basıldığında, sonsuz bir döngüyü durdurun. okumaya hazır giriş akışında bir anahtar varsa gerçek

while (someLoopCondition) 
{ 
    //Do lots of work here 
    if (Console.KeyAvailable) 
    { 
     var consoleKey = Console.ReadKey(true); //true keeps the key from 
               //being displayed in the console 
     if (consoleKey.Key == ConsoleKey.Escape) 
     { 
      //Pause here, ask a question, whatever. 
     } 
    } 
} 

Console.KeyAvailable döner ve bir sivil geçerli: döngüsünde kilit mevcut olup olmadığını

+0

Elbette, ama parçacığı içine almak zorunda olacak. – BradleyDotNET

+0

Yani aradığınız şey kaçış anahtarının basıldığını kontrol etmenin bir yolu, ancak döngü kullanıcının kullanıcısı için beklemeden, değil mi? –

+1

Hmm, kullanıcının manuel olarak bu tuşa basmaya karar vermesi pek olası değildir. Sadece Ctrl + S yerine basmaya gerek yok. –

cevap

9

öğrenmek için bunu yapabilirsin çağrıyı engellemek böylece girişi beklemek için duraklamaz. Çıkış tuşu basılı olup olmadığını kontrol edebilir ve koşul doğruysa ne istersen yapabilir ya da istediğini yapabilirsin.

0

Ron, cevabınız için çok teşekkür ederim. Sorunumun tam çözümünü bulmak için Console.KeyAvalible kullanmak önemliydi. Beklediğim sonuçları üretmek için yaptığım şey buydu. Kullanıcının mevcut işlemi iptal etmek mi yoksa başlangıçtan yeni bir döngü mi başlatmak istediğini kontrol etmek için birkaç yöntem eklemem gerekiyordu.

public static void runUpdater() 
    { 
     int count = 0; 
     while (true) 
     { 
       WriteToConsole("Doing stuff.... Loop# " + count.ToString()); 
       for (int step = 0; step <= int.MaxValue; step++) 
       { 
        if (!breakCurrentOperation()) 
        { 
         WriteToConsole("Performing step #" + step.ToString()); 
         if (step == int.MaxValue) 
         { 
          step = 0; // Re-set the loop counter 
         } 
        } 
        else 
        { 
         break; 
        } 
       } 
       count++; 


       if (!startNewOperation()) 
       { 
        // Noop 
       } 
       else 
       { 
        break; 
       } 

     } 

     WriteToConsole("\nAre you ready to run the database updater again? y/n"); 
     startApplication(ReadFromConsole()); 
    } 

    public static bool startNewOperation() 
    { 
     WriteToConsole("Do you want go back to the main menu or start a new update process? \nType y to start a new update process or n to go to the main menu."); 
     string input = ReadFromConsole(); 

     if (input == "y" || input == "Y") 
     { 
      return false; 
     } 
     else if (input == "n" || input == "N") 
     { 
      return true; // Noop - Restart the Loop from the begining 
     } 
     else 
     { 
      WriteToConsole("Error: Input was not recognized. "); 
      return startNewOperation(); // Recursivly call method untill user enters a logical input 
     } 
    } 

    public static bool breakCurrentOperation() 
    { 
     if (Console.KeyAvailable) 
     { 
      var consoleKey = Console.ReadKey(true); 
      if (consoleKey.Key == ConsoleKey.Escape) 
      { 
       WriteToConsole("Do you want to stop the current process? \nType s to stop or c to continue."); 
       string input = Console.ReadLine(); 
       if (input == "c" || input == "C") 
       { 
        return false; // Continue 
       } 
       else if (input == "s" || input == "S") 
       { 
        return true; // Break the loop 
       } 
       else 
       { 
        WriteToConsole("Error: Input was not recognized, the current process will now continue. Press Esc to stop the operation."); 
       } 
      } 
     } 
     return false; 
    } 

Burada sonuçları:

enter image description here

İlgili konular