I am using multithreading for getting Data for 7 Days,spawning 7 threads so that each thread will get data for a day.
Why multithreading? HttpResponseMessage can not hold more than 2GB of Data
I want to Display the total volume fetched by all the threads. How can I wait for the completion of all the threads?
class Program
{
static long totalVolume = 0;
public static void CallToChildThread(String from, String to)
{
var httpclient = new HttpClient();
String body = query;
var webRequest = new HttpRequestMessage(HttpMethod.Post, api-url){Content = new StringContent(body, Encoding.UTF8, "application/json")};
try
{
var response = httpclient.Send(webRequest);
long size = (long)response.Content.Headers.ContentLength;
totalVolume = totalVolume + size;
}
catch(Exception ex)
{
Console.WriteLine("Exception occured: for "+ from+"-"+to+" , " + ex.Message);
}
}
static void Main(string[] args)
{
to = today;
from = today - 1;
count = 7;
for (int i = 0; i <= count; i++)
{
Thread thread = new Thread(() => CallToChildThread(from, to));
thread.Start();
to = from;
from = to-1;
}
//Code to wait for the completion of all the threads
Console.WriteLine("total Volume fetched is "+totalVolume);
}
}