Issue
I have a downloading Task which downloads data from web in 4 steps, which are defined as async Tasks and runs one by one. Now due to some change I need to capture a user input from a custom dialog in between task 2 and 3. I have written a function to capture input from AlertDialog. The problem is dialog displays in between but it just dont wait and halt processing and process continues without taking user input. The code is something like this:
async void button_click(......){
await function1();
await function2();
await function3();
await function4();
....do the data processing after that.
}
async Task<datatype> function1(){ ...processing step 1 }
async Task<datatype> function2(){
new AlertDialog.Builder(this)
.SetPositiveButton("OK", (sender, args) =>
{
string inputText = txtInput.Text;
})
.SetView(customView)
.Show();
.... some more processing
}
Is there any way I can stop processing untill user input is received from AlertDialog OR any other way of doing the same?
Solution
You could probably do something like this:
public Task<bool> ShowDialog()
{
var tcs = new TaskCompletionSource<bool>();
new AlertDialog.Builder(this)
.SetPositiveButton("OK", (sender, args) =>
{
string inputText = txtInput.Text;
tcs.SetResult(true);
})
.SetView(customView)
.Show();
return tcs.Task;
}
Then you can do:
await function1();
await ShowDialog();
await function2();
Answered By - Cheesebaron
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.