Async loading for winforms

2019-06-09 17:06发布

In (Android) Apps it is quite common to have ListViews loading and being built while the user is interacting with the interface. In Winforms however the tendency seems to be clicking a button and waiting for the result to be fully loaded before the user can continue navigating through the application.

Since the database access that i am currently using is quite slow, i would like to use the database in async methods to enable the user to keep interacting with the interface, while the data is not fully loaded and displayed.

For example i would like to start an asynchronous method in my Form_Load event to keep gathering data. When this method completes, i want to bind the data to some controls - this will (for now) not change the functionality at all. Thus i want the user not to notice any difference (except for the data being shown or not) when handling the application.

Where am i supposed to place the await keyword to accomplish this? I can't place it in my Load event since this needs to complete for the application to behave "normally".

Is it even possible with async methods to have windows forms being fully reactional while not all methods completed, or am i looking at the 'wrong' functionality for my purposes?

Thank you in advance.

Edit: Following Srirams hint, I made the load-event itself an async sub which worked out well. Here is some simple sample code which shows the desired behaviour:

Public Class DelayedLoadingWindow

    Private Async Sub DelayedLoadingWindow_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim awaitedResultOne As Task(Of String) = GetDataOneAsync()
        Label1.Text = Await awaitedResultOne
        Dim awaitedResultTwo As Task(Of String) = GetDataTwoAsync()
        Label2.Text = Await GetDataTwoAsync()
        Dim awaitedResultThree As Task(Of String) = GetDataThreeAsync()
        Label3.Text = Await GetDataThreeAsync()
        Me.Text = "DONE"
    End Sub

    Public Async Function GetDataOneAsync() As Task(Of String)
        Await Task.Delay(2000)
        Return "Async Methods"
    End Function

    Public Async Function GetDataTwoAsync() As Task(Of String)
        Await Task.Delay(2000)
        Return "are"
    End Function

    Public Async Function GetDataThreeAsync() As Task(Of String)
        Await Task.Delay(2000)
        Return "running!"
    End Function
End Class

2条回答
我命由我不由天
2楼-- · 2019-06-09 17:29

You can use the BackgroundWorker Control,

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

You will need to just put the BackgroundWorker Control on your form (backgroundWorker1 in our case), BackgroundWorker Control also supports cancellation, RunWorkerCompleted etc.

private SlowLoadingForm frm;

private void startAsyncButton_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    frm = new SlowLoadingForm();
    frm.show();
    Application.Run(frm);
}
查看更多
我命由我不由天
3楼-- · 2019-06-09 17:34

Where am i supposed to place the await keyword to accomplish this? I can't place it in my Load event since this needs to complete for the application to behave "normally".

Why can't you await inside load event handler? You can do that if you mark the method with async modifier.

private async void Form_Load(object sender, EventArgs e)
{
    //Do something
    var data = await GetDataFromDatabaseAsync();
    //Use data to load the UI
}

This way, you can keep the UI responsive and also execute the time consuming work asynchronously. GetDataFromDatabaseAsync has to be asynchronous (should not block the calling thread).

If this doesn't answers your question, please be more specific.

查看更多
登录 后发表回答