I want to show an indefinite progress bar in a little WPF window of class BackgroundOperationWindow
that I wrote. This is a simple window with an indefinite progress bar, but no "Cancel" button, since there is nothing to be cancelled.
It contains this method:
public async Task PerformAction( Action action )
{
await Task.Run( () => action() );
}
and is used to show that connection to a server is in progress:
async void ConnectToServer( object sender, RoutedEventArgs e )
{
var bo = new BackgroundOperationWindow();
bo.Show();
await bo.PerformAction( () =>
{
// This takes some time...
Server.Connect(...)
} );
}
My problem is: "ConnectToServer" shall not return until the connection is established. (Since the calling window of ConnectToServer
is not yet visible at this time, it is no problem that it will be blocked).
All the examples that I found using async/await use some kind of "Button_Click
" scenario, where it's OK (and intended) that Button_Click
returns before the task is completed.
In other words: I want to get rid of the "async
" in async void ConnectToServer(...)
. ConnectToServer
shall not act asynchronously from its caller's perspective.
What I'm looking for is some kind of "Task.WaitAndKeepTheUIThreadRunningSoThatTheProgressBarAnimates()
"