2

There are many answers out there about how to batch a fairly large DataSet into multiple chunked DataSets.

There are, however, not really solutions about how to do this without locking the UI while chunking these DataSets.

This solution works, but I'm supposed to await the async operation, but I don't know what to await in this code, so the operation executes synchronously:

internal static class ExtensionMethods
{
    internal static async Task<List<DataTable>> CloneTable(DataTable tableToClone, int countLimit)
    {
        List<DataTable> tables = new List<DataTable>();
        int count = 0;
        DataTable copyTable = null;
        foreach (DataRow dr in tableToClone.Rows)
        {
            if ((count++ % countLimit) == 0)
            {
                copyTable = new DataTable();
                copyTable = tableToClone.Clone();
                copyTable.TableName = "TableCount" + count;
                tables.Add(copyTable);
            }
            copyTable.ImportRow(dr);
        }
        return tables;
    }
}

How can I get this to execute asynchronously as opposed to synchronously?

3
  • If you are looking for async for efficiency, another approach could be to use Parallel.ForEach for your table processing: msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx Commented Jun 2, 2016 at 12:38
  • May I suggest you go and read about async/await? The method you post will only run synchronously as there is no await code inside. Commented Jun 2, 2016 at 12:44
  • @DavidG: I use async/await on a bunch of other calls. I was just having issues with this particular case as there are no IO operations :) Commented Jun 2, 2016 at 12:46

1 Answer 1

2

This method doesn't seem to do any IO operations, and it currently runs in a synchronous way. Therefore, I suggest you switch it back to a normal synchronous method, i.e., make it have the following signature:

internal static List<DataTable> CloneTable(DataTable tableToClone, int countLimit)

If all you need is to not hold the UI thread while doing this operation, then all you need to do is use a Thread-pool thread when you invoke this method form the UI event handler like this:

public async void button1_Click(object sender, EventArgs e)
{
    //Execute CloneTable on a thread-pool thread
    var tables = await Task.Run(() => ExtensionMethods.CloneTable(table, 4));

    //Use the tables here. This code will run on the UI thread
    //so you can access any UI elements here
    ...
}

By the way, CloneTable is not currently an extension method because you are not using the this keyword before the tableToClone variable.

Sign up to request clarification or add additional context in comments.

2 Comments

I had no idea I had to specify a this keyword before my type/object to have it qualify as an extension method. I was under the impression this was used to point to a member in the current scope, which is why I don't really use it. Thanks for the lesson :)
You are welcome. You can read about extension methods here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.