سؤال

I am using Simple.Data with SQL Server and I have multiple methods that are independent of each other. On a form I have multiple drop down lists that need to be populated and I think these can be populated asynchronously. I have a repository that returns List of entities.

From my Asp.Net website I call the methods on the repository one by one and bind them to the drop down lists here is a sample code

private void Initialize()
    {
        LoadTechnologies();
        LoadInstallationTypes();
        LoadProvinces();
        LoadYears();            
    }

    private void LoadTechnologies()
    {
        ddlTechnologies.DataSource = _GizRepository.GetTechnologies();
        ddlTechnologies.DataValueField = "Name";
        ddlTechnologies.DataTextField = "Name";
        ddlTechnologies.Items.Insert(0, new ListItem("All", "-1"));
        ddlTechnologies.DataBind();
    }

    private void LoadInstallationTypes()
    {
        ddlInstallationType.DataSource = _GizRepository.GetInstallationTypes();
        ddlInstallationType.DataValueField = "Type";
        ddlInstallationType.DataTextField = "Type";
        ddlInstallationType.Items.Insert(0, new ListItem("Any", "-1"));
        ddlInstallationType.DataBind();
    }

    private void LoadProvinces()
    {
        ddlProvinces.DataSource = _GizRepository.GetProvinces();
        ddlProvinces.DataValueField = "Name";
        ddlProvinces.DataTextField = "Name";
        ddlProvinces.Items.Insert(0, new ListItem("All", "-1"));
        ddlProvinces.DataBind();
    }

    private void LoadYears()
    {
        ddlYearFrom.DataSource = _GizRepository.GetYears();
        ddlYearFrom.DataValueField = "Year";
        ddlYearFrom.DataTextField = "Year";
        ddlYearFrom.DataBind();

        ddlYearTo.DataSource = _GizRepository.GetYears();
        ddlYearTo.DataValueField = "Year";
        ddlYearTo.DataTextField = "Year";
        ddlYearTo.DataBind();
    }

You can see from the code above that all I am doing is fetching some lists from the repository and bind them to the drop downs. I want to execute these methods asynchronously instead of synchronously, Kindly guide how it can be done?

هل كانت مفيدة؟

المحلول

Use async/await but don't return a Task from each routine. The effect is that they run concurrently all awaiting on their own io and then complete on the UI thread.

private void Initialize()
    {
        LoadTechnologies();
        LoadInstallationTypes();
        LoadProvinces();
        LoadYears();            
    }

    // Note that LoadTechnologies will return as soon as it is
    // called. The part after the await will be scheduled on
    // the UI thread after the task completes with the data
    private async Task LoadTechnologies()
    {
        ddlTechnologies.Datasource = 
            await Task.Run(()=>GizRepository.GetTechnologies());
        ddlTechnologies.DataValueField = "Name";
        ddlTechnologies.DataTextField = "Name";
        ddlTechnologies.Items.Insert(0, new ListItem("All", "-1"));
        ddlTechnologies.DataBind();
    }

    private async Task LoadInstallationTypes()
    {
        ...
    }

    ...
}

نصائح أخرى

At the moment there's no way to make Simple.Data do proper asynchronous database calls. Work is starting on version 2 next week, and the first new feature to be added is proper asynchronous operations for those back ends - such as SQL Server - where the ADO provider has asynchronous methods.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top