1

I have to make a c# application which uses REST api to fetch JIRA issues. After I run the tool I am getting the correct output but it is taking a lot of time to display the output. Below is the part of code which is taking the maximum time

var client =new WebClient();

foreach(dynamic i in jira_keys)
{
 issue_id=i.key;
 string rest_api_url="some valid url"+issue_id;
 var jira_response=client.DownloadString(rest_api_url);

 //rest of the processing
}

jira_keys is a JArray. After this there is processing part of the JSON in the for each loop. This is taking a lot of time as the number of jira_keys increase. I cannot apply multi-threading to this since there are shared variable issues. So please someone suggest some way to optimise this.

1
  • 2
    You could run the downloads concurrently. For every jira key, you could for example use DownloadStringAsync. Commented Jun 20, 2017 at 16:45

1 Answer 1

1

Here is an example of how you can fetch the responses from JIRA asynchronously.

var taskList = new List<Task<string>>();
foreach (dynamic i in jira_keys)
{
    issue_id = i.key;
    string rest_api_url = "some valid url" + issue_id;
    var jiraDownloadTask = Task.Factory.StartNew(() => client.DownloadString(rest_api_url));
    taskList.Add(jiraDownloadTask);
}
Task.WaitAll(taskList.ToArray());

//access the results
foreach(var task in taskList)
{
    Console.WriteLine(task.Result);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.