0

I want to call number of methods asynchronously but each method should execute after completion of previous method.

Till now I was wrapped all method under one Master method and called it(Master Method) asynchronously using delegate. now requirement has changed and method which called sequentially in side master method may or may not be called depend on different status. I can achieve it with current architecture by checking different permutation and combination. but I want to execute each method asynchronously but sequentially

This is my current Implementation which run asynchronously (Note: this is an sample code to describe my functionality)

Function 1

private void SaveCustomer(List<Customer> customers)
{
  // Doing some calculation and saving customer in database 
}

Function 2

private decimal CalulateRate(RateInfo applicableRate)
{
  // Calculate rate and some other factors and save in database
}

Function 3

GenerateInvoice(decimal calculatedRate,DateTime effectiveDate, 
                Int32  NumberOfDays)
{
   // generating invoice and saving in database
}

Function 4

GenerateCustomerDocuments(List<DocumentInfo> documentsToGenerate,
                          List<Customer>  customers)
{
   // generating diffrent documents for customers
}

I am calling all above function sequentially from one master Method asynchronously

Private void ExecuteCustomerRegistration(CustomerRegInfo custRegInfo)
{
     //CustomerRegInfo is a class which contains all input parameter            
       object for below methotds

     SaveCustomer(custRegInfo.Customers);
     decimal rate = CalulateRate(custRegInfo.ApplicableRate);
     GenerateInvoice(rate,custRegInfo.EffectiveDate,custRegInfo.NumberOfDays);
     GenerateCustomerDocuments(custRegInfo.DocumentsToGenerate,
                               documentsToGenerate.Customers)

}

Calling ExecuteCustomerRegistration method asynchronously using delegate

protected delegate void delg_DoProcess(CustomerRegInfo custRegInfo);
protected void btnRegisterCustomer_Click(object sender, EventArgs e)
{
     delg_DoProcess doProc = new delg_DoProcess(ExecuteCustomerRegistration);

     CustomerRegInfo custRegInfo = GetCustomersRegInfo();
     doProc.BeginInvoke(custRegInfo, null, null);
}

I want to do same job asynchronously for each methods which are under ExecuteCustomerRegistration method like this

  1. Call SaveCustomer asynchronously
  2. On completion of SaveCustomer call CalulateRate asynchronously
  3. On completion of CalulateRate call GenerateInvoice asynchronously
  4. On completion of GenerateInvoice call GenerateCustomerDocuments asynchronously

Please suggest me how can I do it. currently I am using .net 4.0

Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
  • Can you use a C# 5 compiler and the Microsoft.Bcl.Async package? It'll make things much simpler. – Jon Skeet Feb 10 '14 at 11:42
  • @JonSkeet : Right now not possible to update framework C# 5, I am trying to implement TYY approach – Neeraj Kumar Gupta Feb 10 '14 at 15:46
  • 1
    @NeerajKumarGupta: I wasn't suggesting changing *framework*. C# 5 is a *language* version, not a *framework* version. That's why I suggested Microsoft.Bcl.Async - that allows you to use async/await with .NET 4, so long as you have a C# 5 compiler. – Jon Skeet Feb 10 '14 at 15:53

1 Answers1

0

You can chain multiple tasks.

var firstTask  = new Task(() => SaveCustomer(custRegInfo.Customers);
var SecondTask = firsttask.ContinueWith((t)=> CalulateRate(custRegInfo.ApplicableRate)).
firstTask.Start();

Note this is just an example, not saying this is how you should exactly do it. But it gives you an idea of how to use Task and Continuation. Which should serve your purpose.

TYY
  • 2,702
  • 1
  • 13
  • 14