public void AddData(string text, TopicResponseTopicSortingItem trt)
 {
     this.Dispatcher.Invoke(() =>
     {
         var i = lst_Results.Items.Add(trt);
         //resultsArea.Items.GetItemAt(i)
     });
 }
        private void GetDataAndUpdate(string requestId, string title, int wait, int topicCount)
        {
            // next try once a minute to fetch contents
            using (var client = new WebClient())
            {
                // setting correct headers
                client.Headers.Add("Ocp-Apim-Subscription-Key", key1);
                client.Headers.Add("Content-Type", "application/json");
                //client.Headers[HttpRequestHeader.ContentType] = "application/json";
                client.Headers.Add("Accept", "application/json");

                for (int i = 0; i < 15 + wait; i++)
                {
                    try
                    {
                        if (wait > i)
                        {
                            throw new Exception(title + ": Need to wait a while.");
                        }
                        var responseJson = client.DownloadString(requestId);

                        var jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject <TopicResponse>(responseJson);

                        switch (jsonData.status.ToLower())
                        {
                        case "running":
                            throw new Exception(title + ": Still running...");
                            break;

                        case "notstarted":
                            throw new Exception(title + ": Not started yet!");
                            break;

                        case "succeeded":
                            AddMessage(title + ": " + jsonData.status + " " + jsonData.message);

                            var sortedTopics = jsonData.operationProcessingResult.topics.ToList();
                            sortedTopics.Sort((p1, p2) => p1.score.CompareTo(p2.score));
                            sortedTopics.Reverse();
                            sortedTopics = sortedTopics.Take(topicCount).ToList();

                            foreach (TopicResponseTopic trt in sortedTopics)
                            {
                                TopicResponseTopicSortingItem trtsi = new TopicResponseTopicSortingItem(trt);
                                trtsi.title = title;
                                AddData(title, trtsi);
                            }

                            break;

                        default:
                            throw new Exception(title + ": Don't know what happened.");
                            break;
                        }

                        break;
                    }
                    catch (Exception ex)
                    {
                        AddMessage(ex.Message);
                        Thread.Sleep(60000);
                    }
                }
            }
        }