Ejemplo n.º 1
0
        private async void PostRequest()
        {
            try
            {
                var content = new StringContent(JsonConvert.SerializeObject(imageDirectory), Encoding.UTF8, "application/json");
                HttpResponseMessage httpResponse;
                try
                {
                    httpResponse = await client.PostAsync(url, content, cts.Token);
                }
                catch (HttpRequestException)
                {
                    await disp.BeginInvoke(new Action(() =>
                    {
                        MessageBox.Show("NO CONNECTION", "Warning");
                        Stop();
                    }));

                    return;
                }

                if (httpResponse.IsSuccessStatusCode)
                {
                    var items = JsonConvert.DeserializeObject <List <RecognitionInfo> >(httpResponse.Content.ReadAsStringAsync().Result);
                    foreach (var item in items)
                    {
                        await disp.BeginInvoke(new Action(() =>  //await ???
                        {
                            ClassesImages.Add(item);
                            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ClassesImages"));
                            Pair <string, int> p;
                            try
                            {
                                p = AvailableClasses.Single(i => i.Item1 == item.Class);
                                p.Item2 += 1;
                            }
                            catch (InvalidOperationException)
                            {
                                AvailableClasses.Add(new Pair <string, int>(item.Class, 1));
                                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AvailableClasses"));
                            }
                        }));
                    }
                    isRunning = false;
                }
            }
            catch (OperationCanceledException)
            {
                await disp.BeginInvoke(new Action(() =>
                {
                    MessageBox.Show("RECOGNITION STOPPED", "Warning");
                }));
            }
        }
Ejemplo n.º 2
0
        private async void PostRequest()
        {
            try
            {
                //MessageBox.Show(imageDirectory);
                var images = from file in Directory.GetFiles(imageDirectory) // пустой путь - throw exception
                             where file.Contains(".jpg") ||
                             file.Contains(".jpeg") ||
                             file.Contains(".png")
                             select file;
                Dictionary <string, string> ToServer = new Dictionary <string, string>();
                foreach (var i in images)
                {
                    ToServer.Add(i, Convert.ToBase64String(File.ReadAllBytes(i)));
                }

                var content = new StringContent(JsonConvert.SerializeObject(ToServer), Encoding.UTF8, "application/json");
                HttpResponseMessage httpResponse;
                try
                {
                    httpResponse = await client.PostAsync(url, content, cts.Token);
                }
                catch (HttpRequestException)
                {
                    await disp.BeginInvoke(new Action(() =>
                    {
                        MessageBox.Show("NO CONNECTION", "Warning");
                        Stop();
                    }));

                    return;
                }

                if (httpResponse.IsSuccessStatusCode)
                {
                    var items = JsonConvert.DeserializeObject <List <RecognitionContract> >(httpResponse.Content.ReadAsStringAsync().Result);
                    foreach (var item in items)
                    {
                        await disp.BeginInvoke(new Action(() =>  //await ???
                        {
                            ClassesImages.Add(item);
                            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ClassesImages"));
                            Pair <string, int> p;
                            try
                            {
                                p = AvailableClasses.Single(i => i.Item1 == item.Class);
                                p.Item2 += 1;
                            }
                            catch (InvalidOperationException)
                            {
                                AvailableClasses.Add(new Pair <string, int>(item.Class, 1));
                                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AvailableClasses"));
                            }
                        }));
                    }
                    isRunning = false;
                }
            }
            catch (OperationCanceledException)
            {
                await disp.BeginInvoke(new Action(() =>
                {
                    MessageBox.Show("RECOGNITION STOPPED", "Warning");
                }));
            }
        }