1

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?

Afraz Ali
  • 2,672
  • 2
  • 27
  • 47

2 Answers2

0

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()
    {
        ...
    }

    ...
}
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
  • Thanks a lot, I understand that these will be async calls but not parallel. Can you help as to how we can make them parallel as well? – Afraz Ali Aug 06 '13 at 09:34
  • They will be parallel. Task.Run grabs a spare thread from the thread pool. Every task is run in a separate thread. – bradgonesurfing Aug 06 '13 at 10:12
  • Most of the time spent here will be waiting on IO. When the task is awaiting on IO the thread is returned to the thread pool. When the task needs to be continued another thread is grabbed from the thread pool. – bradgonesurfing Aug 06 '13 at 10:14
  • I'd make the methods return `Task` so that someone who wants to await them actually can do that. As long as you don't await the calls in `Initialize` they will still be executed in parallel. I'd also add the "Async" postfix to the methods to follow the standard. – cremor Aug 06 '13 at 10:18
  • @cremor Added the Task return type. – bradgonesurfing Aug 06 '13 at 10:31
  • If your methods are really running asynchronously, it is possible that the Initialize method will return without any of the tasks has completed. – Paulo Morgado Aug 06 '13 at 23:22
  • It certainly will. If you care about that there are static methods on Task to wait for completion of a bunch of tasks. HOwever if the tasks themselves update the UI there is nothing to wait for. – bradgonesurfing Aug 07 '13 at 03:42
0

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.

Mark Rendle
  • 9,274
  • 1
  • 32
  • 58
  • Thanks Mark but I suppose the answer suggested here will work well in any case although the Async thread will not be at simple.data level but at the repository caller. – Afraz Ali Aug 07 '13 at 02:30