I have a requirement where I have pull data from Sailthru API.The problem is it will take forever if I make a call synchronously as the response time depends on data.I have very much new to threading and tried out something but it didn't seems to work as expected. Could anyone please guide. Below is my sample code
public void GetJobId() { Hashtable BlastIDs = getBlastIDs(); foreach (DictionaryEntry entry in BlastIDs) { Hashtable blastStats = new Hashtable(); blastStats.Add("stat", "blast"); blastStats.Add("blast_id", entry.Value.ToString()); //Function call 1 //Thread newThread = new Thread(() => //{ GetBlastDetails(entry.Value.ToString()); //}); //newThread.Start(); } } public void GetBlastDetails(string blast_id) { Hashtable tbData = new Hashtable(); tbData.Add("job", "blast_query"); tbData.Add("blast_id", blast_id); response = client.ApiPost("job", tbData); object data = response.RawResponse; JObject jtry = new JObject(); jtry = JObject.Parse(response.RawResponse.ToString()); if (jtry.SelectToken("job_id") != null) { //Function call 2 Thread newThread = new Thread(() => { GetJobwiseDetail(jtry.SelectToken("job_id").ToString(), client,blast_id); }); newThread.Start(); } } public void GetJobwiseDetail(string job_id, SailthruClient client,string blast_id) { Hashtable tbData = new Hashtable(); tbData.Add("job_id", job_id); SailthruResponse response; response = client.ApiGet("job", tbData); JObject jtry = new JObject(); jtry = JObject.Parse(response.RawResponse.ToString()); string status = jtry.SelectToken("status").ToString(); if (status != "completed") { //Function call 3 Thread.Sleep(3000); Thread newThread = new Thread(() => { GetJobwiseDetail(job_id, client,blast_id); }); newThread.Start(); string str = "test sleeping thread"; } else { string export_url = jtry.SelectToken("export_url").ToString(); TraceService(export_url); SaveCSVDataToDB(export_url,blast_id); } } I want the Function call 1 to start asynchronously(or may be after gap of 3 seconds to avoid load on processor). In Function call 3 I am calling the same function again if the status is not completed with delay of 3 seconds to give time for receiving response.
Also correct me if my question sounds stupid.