public static void DataThread(object obj) { AsyncData adata = obj as AsyncData; adata.semaphore.WaitOne(); try { WebRequest request = WebRequest.Create(adata.url); WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream); String responseString = reader.ReadToEnd(); if (adata.index < 0) { Study study = new Study(); study.study = responseString; lock (adata.studies) { adata.studies.Add(study); } } else { lock (adata.studies[adata.index].patient) { adata.studies[adata.index].patient = responseString; } } } response.Close(); } catch (Exception e) { Log.WriteLog(e.Message); } adata.semaphore.Release(); }
protected string StudiesList(string host) { int simultaneousNum = config.GetConfigInt("simultaneousThreads", 10); int period = 0; try { period = int.Parse(Request.QueryString["period"]); } catch { } string[] studyIDs = new string[0]; try { studyIDs = JArray.Parse(Study.GetStudiesIDs(host, period)).ToObject <string[]>(); } catch { Log.WriteLog("Failed to get studies' IDs"); } Semaphore semaphore = new Semaphore(simultaneousNum, simultaneousNum); List <Study> list = new List <Study>(studyIDs.Length); List <Thread> threads = new List <Thread>(studyIDs.Length); foreach (string item in studyIDs) { try { AsyncData ad = new AsyncData(); ad.semaphore = semaphore; ad.url = host + "studies/" + item; ad.studies = list; ad.index = -1; Thread thread = new Thread(Study.DataThread); threads.Add(thread); thread.Start(ad); } catch (Exception e) { Log.WriteLog("Failed to get study " + item + ": " + e.Message); } } bool allDone = false; while (!allDone) { bool join = true; foreach (Thread thread in threads) { join = thread.Join(30); if (!join) { break; } } allDone = join; } threads.Clear(); semaphore = new Semaphore(simultaneousNum, simultaneousNum); threads = new List <Thread>(studyIDs.Length); for (int i = 0; i < list.Count; i++) { string parent = ""; try { AsyncData ad = new AsyncData(); ad.semaphore = semaphore; parent = (string)JObject.Parse(list[i].study)["ParentPatient"]; ad.url = host + "patients/" + parent; ad.studies = list; ad.index = i; Thread thread = new Thread(Study.DataThread); threads.Add(thread); thread.Start(ad); } catch (Exception e) { Log.WriteLog("Failed to get study's parent patient " + parent + ": " + e.Message); } } allDone = false; while (!allDone) { bool join = true; foreach (Thread thread in threads) { join = thread.Join(30); if (!join) { break; } } allDone = join; } //append description Hashtable ht = IDDesc.Load(config.GetConfigSourceOption("dataDrive") + "\\"); try { for (int i = 0; i < list.Count; i++) { try { JObject jpatient = JObject.Parse(list[i].patient); //string id = (string)jpatient["MainDicomTags"]["PatientID"]; string id = (string)jpatient["ID"]; if (ht.ContainsKey(id)) { string escid = JsonConvert.ToString(ht[id]); list[i].patient = list[i].patient.Insert(list[i].patient.LastIndexOf('}'), ",\"iddesc\":" + escid); } } catch { } } } catch { } StringBuilder json = new StringBuilder(); json.Append("["); if (list.Count > 0) { json.Append(list[0].GetJSON()); } for (int i = 1; i < list.Count; i++) { json.Append("," + list[i].GetJSON()); } json.Append("]"); return(json.ToString()); }