2011-10-22 93 views
1

我需要能夠在等待用戶輸入時停止方法。在繼續執行方法之前等待可變時間完成

我試着用while (true)循環檢查是否設置了一個表示操作已完成的布爾值,但正如我預測的那樣,它使應用程序無法響應。

如何在設置了變量後暫停和恢復方法,而無需再次調用方法,並且不會使整個應用程序無響應。

這裏基本上什麼程序呢

openFile method called
openFile method determines whether file has a password
if it does, display an alert to the user requesting the password

這裏的問題,停止該方法,直到密碼已經輸入。

任何幫助表示讚賞。

回答

0

你不能。你需要將它分成兩種方法,一種做初始檢查,然後提示輸入密碼,然後第二種使用密碼來獲取文件。

+0

是的,那是最好的選擇。 –

2

爲什麼要停止該方法?你可以使用委託。您顯示警報視圖並註冊警報視圖的代表。委託註冊didDismissAlertViewWithButtonIndex方法,並根據按鈕進行操作。如果密碼輸入正確並點擊OKAY按鈕,則可以繼續處理。

0

你可能需要像下面這樣的東西。

class Program 
    { 
     static void Main(string[] args) 
     { 
     } 

     void YourFileReadMethod() 
     { 
      string password = string.Empty; 
      bool isPasswordProtected = CheckFileIsPasswordProtected(); 
      if (isPasswordProtected) 
      { 
       Form passwordFrm = new Form();//This is your password dialogue 
       DialogResult hasEntered = DialogResult.No; 
       do 
       { 
        hasEntered = passwordFrm.ShowDialog(); 
        password = (hasEntered == DialogResult.Yes) ? 
         passwordFrm.passwordTxt//password property/control value; 
         : string.Empty; 
       } while (hasEntered != DialogResult.Yes); 
      } 
      ReadFileMethod(password); 
     } 

     private void ReadFileMethod(string password) 
     { 
      //use the password value to open file if not String.Empty 
     } 
     bool CheckFileIsPasswordProtected() 
     {    
      //your logic which decides whether the file is password protected or not 
      //return true if password is required to open the file 

      return true; 
     } 
    } 
+0

這不是Objective-C,無論如何不會在可可中工作。 – Flyingdiver

相關問題