Search code examples
c#.netmultithreadingdata-access-layersplash-screen

Best way to run Database access tasks, that return objects, in separate thread keeping UI responsive?


[Windows forms application & .NET 4.0]

I need to execute database access methods that return objects (either list of classes or simple classes).

Also i need to open forms that are responsive while main thread does initialization.

I need to run these on separate threads keeping the User Interface responsive and of course to be able to pass the results back to main thread for UI updates.

I have been reading books regarding the various ways for this.

I understand that my job can be done by:

  • BackGroundWorker
  • Thread Class
  • Task Class

Which one i should dive into ?

Update: using the suggested Task class i am getting errot for cross thread safety using this:

private void BtnCheckClick(object sender, EventArgs e)
{
    var itm =   Task<JDEItemLotAvailability>.Factory.StartNew(() =>
                             Dal.GetLotAvailabilityF41021(
                                                         txtLot.Text,
                                                         cmbMcu.SelectedItem.ToString(),
                                                         cmbLocn.SelectedItem.ToString())
                            );
       lblDescriptionValue.Text = itm.Result.Description;
       lblItemCodeValue.Text = itm.Result.Code;
       lblQuantityValue.Text = itm.Result.AvailableQuantity.ToString();
       LotFocus(true);
}

On the above exmaple i am getting the exception in cmbMcu control not the txtLot.


Solution

  • I would use the Task class, it's really easy to synchronize it and it already provides a support for returning objects.

    var task = Task.Factory.StartNew(
        () => GetDatabaseData(someArguments),
        TaskCreationOptions.LongRunning);
    
    // Example method
    public DataSet GetDatabaseData(object args) { ... }
    

    this this tells a scheduler to create and begin a new task and gives it a hint that it might be a good idea not to use a thread-pool thread, if the scheduler uses a thread-pool. Anyway you can now decide how do you want to synchronize.

    For example to achieve similar behaviour as in Gregor Primar's answer, you can set up a continuation using ContinueWith method as follows,

    task.ContinueWith(oldTask => ProcessReturnedData(oldTask.Result));
    
    // Example method
    public IEnumerable<SomeEntity> ProcessReturnedData(DataSet data) { ... }
    

    which will schedule calling the ProcessReturnedData method after the task object has done executing. Note that this will be called even if task fails for some reason, so it may not be always a good solution - or you would have to do some checks in the provided delegate.

    If you want to do a non-blocking wait on the main thread and use the returned object there, you can simply use the Wait method.

    task.Wait(); // Makes current thread wait until the task is comnpleted.
    DataSet result = task.Result; // Accessing the result object.