I am trying to figure it out that why these two Async method not working parallel. Note that each method returns some html, and for this Iteration inside it (its demo code) (original code is in my .Net project).
Each method individually takes 9 seconds to complete.
Both method are Async even it takes 18 seconds to complete if its invoke and run in single method. It means it not runs parallel.
Overall what i want is, It should take 9 second to complete both the method. How it is possible? Please suggest to run both method parallel. (I want this because in my project there are many method inside one method, i want all runs parallel)
Thanks in advance.
Please review below code:
namespace LearnAynchStackOverflow
{
class Program
{
static async Task Main(string[] args)
{
var startTime = DateTime.Now;
Console.WriteLine($"Start time: {startTime}");
var emailHtml = EmailTemplate.EmailHtml();
var smsHtml = SmsTemplate.SmsHtml();
var f = await Task.WhenAll(emailHtml, smsHtml);
var endTime = DateTime.Now;
Console.WriteLine($"End time: {endTime}");
Console.ReadLine();
}
}
public class EmailTemplate
{
//This method takes 9 seconds to complete
public static async Task<string> EmailHtml()
{
string str = string.Empty;
for (int i = 0; i < 25000; i++)
{
str += "html1" + i + " ";
str += "html1" + i + " ";
}
var a = await Task.FromResult(str);
return a;
}
}
public class SmsTemplate
{
//This method takes 9 seconds to complete
public static async Task<string> SmsHtml()
{
string str = string.Empty;
for (int i = 0; i < 25000; i++)
{
str += "html2" + i + " ";
str += "html2" + i + " ";
}
var z = await Task.FromResult(str);
return z;
}
}
}
And Below is my console result which takes 18 seconds: