0

I'm taught asynchronous programming helps to spawn multiple threads so that the async thread never impacts the UI and the subsequent lines of code need not wait until the completion of the previous thread. Now the idea is I'm calling a flyout asynchronously and while it is being called I'm wanting to hide the bottom appbar. But surprisingly when implemented, the appbar is not hidden until the flyout is opened and dismissed. Couldn't understand as to why. Here's the abstract piece of code. your inputs will help me to understand async processes better.

private async void OnClick(object sender, TappedRoutedEventArgs e) 
{

    var flyout = new cmpWebA.Flyout();

    await flyout.ShowAsync();

    this.BottomAppBar.IsOpen = false;

}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • Just forgot to add one more input. The flyout here is being triggered at the button click from the bottom appbar –  Aug 12 '13 at 13:49
  • 4
    We have no idea what `Flyout.ShowAsync` does, but I think you've misunderstood what async/await does... in particular, it never creates a new thread automatically. – Jon Skeet Aug 12 '13 at 13:53

2 Answers2

2

Invoking an asynchronous operation involves two parts: starting it, and awaiting its completion.

You code currently starts the operation flyout.ShowAsync();, then awaits its completion (await) and then hides the bottom bar (this.BottomAppBar.IsOpen = false;).

If you want to hide the bottom bar while ShowAsync is running, hide it before you start it and show it when it's completed:

private async void OnClick(object sender, TappedRoutedEventArgs e)
{
    this.BottomAppBar.IsOpen = false;

    var flyout = new cmpWebA.Flyout();
    await flyout.ShowAsync();

    this.BottomAppBar.IsOpen = true;
}

You can also start ShowAsync first, then hide the bottom bar, then await completion and then show the bottom bar:

private async void OnClick(object sender, TappedRoutedEventArgs e)
{
    var flyout = new cmpWebA.Flyout();
    var task = flyout.ShowAsync();

    this.BottomAppBar.IsOpen = false;

    await task;

    this.BottomAppBar.IsOpen = true;
}
dtb
  • 213,145
  • 36
  • 401
  • 431
0

Asynchronousity isn't the same thing as concurrency. An asynchronous operation certainly can be implemented with a thread, it however isn't all that common. You for example want to use await when you are waiting for a slow I/O operation to complete. Which is done by built-in support for overlapped I/O in the operating system, it doesn't require a thread.

If you want true concurrency, having more than one thread working on getting a job done, then there is no substitute for actually creating the threads. Or Tasks.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Your comment opened me up to explore more about the differences between Asynchronous / Parallel / Concurrent Programming & found this http://stackoverflow.com/questions/4844637/what-is-the-difference-between-concurrency-parallelism-and-asynchronous-methods however if you could add more clarity on the top of this I'd still appreciate –  Aug 13 '13 at 07:54