Reputation: 10460
I need to be able to halt a method while it waits for user input.
I tried using a while (true)
loop to check to see if a boolean was set indicating that the operation had completed, but as I predicted, it made the application non-responsive.
How do you halt and resume a method after a variable has been set, without calling the method again, and without making the entire application non-responsive.
Here's basically what the program does
openFile method called
openFile method determines whether file has a password
if it does, display an alert to the user requesting the password
Here's the problem, halting the method until the password has been entered.
Any help appreciated.
Upvotes: 1
Views: 233
Reputation: 812
You may need something like below.
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;
}
}
Upvotes: 0
Reputation: 1705
Why do you want to halt the method? You could use a delegate. You present the alert view and register the delegate for the alert view. The delegate registers for the didDismissAlertViewWithButtonIndex method and acts according to the button. If the password was entered correctly and the OKAY button was tapped, you can continue with your process.
Upvotes: 2
Reputation: 2142
You can't. You need to split it up into two methods, one that does the initial check and then prompts for the password, then the second that uses the password to get the file.
Upvotes: 0