【问题标题】:Wait for something of variable time to complete before continuing method在继续方法之前等待可变时间完成
【发布时间】:2011-10-22 13:14:06
【问题描述】:

我需要能够在等待用户输入时停止方法。

我尝试使用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

这就是问题所在,在输入密码之前暂停该方法。

任何帮助表示赞赏。

【问题讨论】:

    标签: objective-c multithreading cocoa asynchronous


    【解决方案1】:

    为什么要停止该方法?您可以使用委托。您呈现警报视图并为警报视图注册代理。委托注册 didDismissAlertViewWithButtonIndex 方法并根据按钮执行操作。如果密码输入正确并且点击 OKAY 按钮,您可以继续您的流程。

    【讨论】:

      【解决方案2】:

      你不能。您需要将其拆分为两种方法,一种是进行初始检查,然后提示输入密码,然后是使用密码获取文件的第二种方法。

      【讨论】:

      • 是的,这是最好的方法。
      【解决方案3】:

      您可能需要以下内容。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多