2016-01-06 42 views
0

我正在編寫一個C#應用程序。我有一個從數據庫驗證數據的主要過程。如果我有用戶輸入,計時器會檢查每100個滴答。在我得到用戶輸入後,我的主進程繼續進行(它在開始時等待用戶輸入)。在驗證並做了多件事情之後,方法就完成了。事情是,我希望它再次開始等待下一個輸入。重新啓動方法

我正在考慮在每個可能的結束過程中再次調用該方法。我有一種感覺,這會創造一個資源沉重的計劃,雖然(不是最壞的事情,但最好沒有壞習慣比幾個權利?)。

例子:

bool cont = false; 

public void process() 
{  
    while (cont == false) {;} 

    //Various sorting criteria that all end up with cont = false; process(), the userinput has been processed. 

} 

timer1 tick event 
{ 
    if (userinput) 
     cont = true; 
} 
+0

這裏的問題究竟是什麼? –

+0

我的不好。我想知道是否有任何適當的方法讓它重新開始。我覺得我的方式(不斷調用方法)會非常耗費資源。 – Gijsbouwse

+0

你可以使用遞歸 –

回答

0

因爲你不看見你將如何獲取用戶輸入的,我不實現這一個。但你的問題的主要邏輯是:

class MainClass 
{ 
    public static void Main() 
    { 
     MyRecursiveFunction(); 
     AfterUserInput(); 
    } 

    public static void MyRecursiveFunction() 
    { 
     if (userinput) 
     { return; } 

     // Waits 100 ticks to check again 
     Thread.Sleep(new TimeSpan(100)); 

     MyRecursiveFunction(); 
    } 

    public static void AfterUserInput() 
    { 
     // All that you need to do after the user input 
    } 
}