Here are the examples of the csharp api System.Threading.Tasks.TaskCompletionSource.TrySetResult(LoginDialogData) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
19
Source : LoginDialog.cs
with Apache License 2.0
from beckzhu
with Apache License 2.0
from beckzhu
internal Task<LoginDialogData> WaitForButtonPressAsync()
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
this.Focus();
if (string.IsNullOrEmpty(this.PART_TextBox.Text) && !this.ShouldHideUsername)
{
this.PART_TextBox.Focus();
}
else
{
this.PART_TextBox2.Focus();
}
}));
TaskCompletionSource<LoginDialogData> tcs = new TaskCompletionSource<LoginDialogData>();
RoutedEventHandler negativeHandler = null;
KeyEventHandler negativeKeyHandler = null;
RoutedEventHandler affirmativeHandler = null;
KeyEventHandler affirmativeKeyHandler = null;
KeyEventHandler escapeKeyHandler = null;
Action cleanUpHandlers = null;
var cancellationTokenRegistration = this.DialogSettings.CancellationToken.Register(() =>
{
cleanUpHandlers();
tcs.TrySetResult(null);
});
cleanUpHandlers = () =>
{
this.PART_TextBox.KeyDown -= affirmativeKeyHandler;
this.PART_TextBox2.KeyDown -= affirmativeKeyHandler;
this.KeyDown -= escapeKeyHandler;
this.PART_NegativeButton.Click -= negativeHandler;
this.PART_AffirmativeButton.Click -= affirmativeHandler;
this.PART_NegativeButton.KeyDown -= negativeKeyHandler;
this.PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler;
cancellationTokenRegistration.Dispose();
};
escapeKeyHandler = (sender, e) =>
{
if (e.Key == Key.Escape)
{
cleanUpHandlers();
tcs.TrySetResult(null);
}
};
negativeKeyHandler = (sender, e) =>
{
if (e.Key == Key.Enter)
{
cleanUpHandlers();
tcs.TrySetResult(null);
}
};
affirmativeKeyHandler = (sender, e) =>
{
if (e.Key == Key.Enter)
{
cleanUpHandlers();
tcs.TrySetResult(new LoginDialogData
{
Username = this.Username,
SecurePreplacedword = this.PART_TextBox2.SecurePreplacedword,
ShouldRemember = this.RememberCheckBoxChecked
});
}
};
negativeHandler = (sender, e) =>
{
cleanUpHandlers();
tcs.TrySetResult(null);
e.Handled = true;
};
affirmativeHandler = (sender, e) =>
{
cleanUpHandlers();
tcs.TrySetResult(new LoginDialogData
{
Username = this.Username,
SecurePreplacedword = this.PART_TextBox2.SecurePreplacedword,
ShouldRemember = this.RememberCheckBoxChecked
});
e.Handled = true;
};
this.PART_NegativeButton.KeyDown += negativeKeyHandler;
this.PART_AffirmativeButton.KeyDown += affirmativeKeyHandler;
this.PART_TextBox.KeyDown += affirmativeKeyHandler;
this.PART_TextBox2.KeyDown += affirmativeKeyHandler;
this.KeyDown += escapeKeyHandler;
this.PART_NegativeButton.Click += negativeHandler;
this.PART_AffirmativeButton.Click += affirmativeHandler;
return tcs.Task;
}