0

Im trying to move data from SQL to MongoDB. Question is related to C# Async Await. What I do is collect 2000 documents first and then insert into MongoDB. I am awaiting my insert call. What I want to do here is instead of waiting I want to continue collecting next 2000 documents but only call the SaveToCollection method only if last call is returned.

int counter = 0, totalCounter = 0;
List<BsonDocument> documentList = new List<BsonDocument>();
while (rdr.Read())
{
    Dictionary<string, object> obj = new Dictionary<string, object>();
    for (int lp = 0; lp < rdr.FieldCount; lp++)
    {
        if (rdr.GetValue(lp) != DBNull.Value)
            obj.Add(rdr.GetName(lp).ToLower(), rdr.GetValue(lp));
        else
            obj.Add(rdr.GetName(lp).ToLower(), "");
    }
    documentList.Add(new BsonDocument(obj));
    counter++;
    if (counter == 2000)
    {
        /* I dont want to wait here. Continue to get 2000 more documents but wait if previous call is not completed yet.*/
        if (SaveToCollection(documentList, MongoCollectionName).GetAwaiter().GetResult())
        {
            totalCounter = totalCounter + counter;
        }
        documentList.Clear();
        counter = 0;
    }
}
Krishna
  • 529
  • 3
  • 13
  • 1
    A very simplistic solution would be: Task task = null; while (rdr.Read()) ... if (counter == 200) { task?.Wait(); if (task.Status == TaskStatus.RunToCompletion) { totalCounter = totalCounter + 200; } task = SaveToCollection(documentList, MongoCollectionName); documentList = new List(); counter = 0; } – Marco Feb 22 '18 at 16:12
  • @Marco This is what im looking for. Thanks. I will give it a try. – Krishna Feb 22 '18 at 16:14
  • @Marco works like a charm. Thanks again. – Krishna Feb 22 '18 at 17:41

1 Answers1

0

I personally would use Task.Run(); and save the returned task object. You can then await it on the next loop around before continuing

simonalexander2005
  • 4,338
  • 4
  • 48
  • 92