3

I have a method that is called after the initialization component, the method signature is like:

public async void method_name ()
{
    // code ...
}

Inside that method, I have a loop running with 4 different if statements. I need the loop to pause at each if statements and wait for the user to press a button. Since pressing that button will add info and stuff. Once the button is pressed, I want the loop to continue and of course pause at the next if statements.

I thought about doing it like await Task.Delay(30000); but if the user is done entering the info before that timer is over, he/she will just be waiting. and That's not efficient.

5
  • 3
    This sounds like an XY problem, is it not possible to get the user input up front? Commented Jun 18, 2015 at 7:05
  • Can you explain what type of computation you are trying to do? Commented Jun 18, 2015 at 7:08
  • 1
    async void is meant for event handlers. You probably shouldn't be using it anywhere else. Commented Jun 18, 2015 at 7:09
  • Well it's a game, so you have the select your move, and then when you click a button it enters the move and that's when the loop should get the the values, do what it's suppose to then move on. I hope that clears it up Commented Jun 18, 2015 at 7:23
  • What kind of project is this? most game engines use their own game loop in which case you just need to capture the users input. If it is something such as winforms then you may just want to either invent your own game loop (increment a variable such as momentum on the button click to be updated next frame) or update the game on a button click in a turn based style. Note: The latter aren't suited to game development and I'd highly suggest something such as mono/xna/unity/udk Commented Jun 18, 2015 at 7:30

1 Answer 1

8

You can do that with TaskCompletionSource. Create it and await its Task property and when a button is clicked use it to complete that task. That allows to asynchronously wait for the user input without blocking a thread or using Task.Delay.

TaskCompletionSource<bool> _tcs;

async Task Foo()
{
    // stuff
    _tcs = new TaskCompletionSource<bool>();
    await _tcs.Task
    // other stuff
}

void ButtonClicked(object sender, EventArgs e)
{
    _tcs.SetResult(false);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Great answer - wish I could +2
hmm.. since i'm new to his whole UI coding and C#, do i just call the method foo, in the nested if statement within then loop, and that will pause the loop till i click the button?
@GK28 Foo is your method, it's method_name. Your "pause" is at await _tcs.Task and you can put it before the if (or after, depends on your logic). If you need this for 4 different buttons use 4 different TCSs.
oh I see what you mean, 1 sec, going to try it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.