public void AppStart() { Task.Factory.StartNew(() => { try { while (true) { BatchWoker.ExecuteBatchRequest(); } } catch (Exception e) { Console.WriteLine(e); throw; } }); }
public StatusEnum Test_GetBatchStatus(bool checkCompleted) { try { //submiting batch request var submitResponse = Test_SubmitBatch(); if (submitResponse != null) { var batchGuid = submitResponse.BatchGuid; var controller = new DownloadController { Request = new HttpRequestMessage() }; controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); var dequeueWorker = Task.Factory.StartNew(() => { BatchWoker.ExecuteBatchRequest(); }); //if test is for "COMPLETED" batch then wait for all tasks to finish, else provide the status immediately if (checkCompleted) { dequeueWorker.Wait(); if (dequeueWorker.IsCompleted) { //calling target action var response = controller.GetBatchStatus(submitResponse.BatchGuid.ToString()); if (response != null) { var responseReadTask = Task.Factory.StartNew(() => { return(response.Content.ReadAsStringAsync()); }); var result = responseReadTask.Result.Result; if (string.IsNullOrEmpty(result)) { throw new Exception("empty response from batch status request"); } else { try { return(JsonConvert.DeserializeObject <StatusEnum>(result)); } catch (Exception e) { throw new Exception("failed to deserialize batch status response", e); } } } else { throw new Exception("null response from batch status request"); } } else { throw new Exception("failed to execute batch request"); } } else { //calling target action var response = controller.GetBatchStatus(submitResponse.BatchGuid.ToString()); if (response != null) { var responseReadTask = Task.Factory.StartNew(() => { return(response.Content.ReadAsStringAsync()); }); var result = responseReadTask.Result.Result; if (string.IsNullOrEmpty(result)) { throw new Exception("empty response from batch status request"); } else { try { return(JsonConvert.DeserializeObject <StatusEnum>(result)); } catch (Exception e) { throw new Exception("failed to deserialize batch status response", e); } } } else { throw new Exception("null response from batch status request"); } } } else { throw new Exception("null response for batch submit request"); } } catch (Exception e) { Console.WriteLine(e); throw; } }