Beispiel #1
0
        private async void ButtonAsync_Click(object sender, EventArgs e)
        {
            Progress <ProgressReportModel> progress = new Progress <ProgressReportModel>();

            progress.ProgressChanged += ReportProgress;

            var watch = System.Diagnostics.Stopwatch.StartNew();

            if (cts != null)
            {
                cts.Dispose();
            }

            cts = new CancellationTokenSource();

            try
            {
                var results = await RunDownloadAsync(progress, cts.Token);

                PrintResults(results);
            }
            catch (OperationCanceledException)
            {
                TextBoxResult.AppendLine($"The async download was canceled.");
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            Application.DoEvents();

            TextBoxResult.AppendLine($"Total execution time: {elapsedMs}");
        }
Beispiel #2
0
        private void PrintResults(List <WebSiteDataModel> results)
        {
            TextBoxResult.Text = "";

            foreach (var item in results)
            {
                TextBoxResult.AppendLine($"{item.WebsiteUrl} downloaded: {item.WebsiteData.Length} characters long.");
            }
        }
Beispiel #3
0
        private async void ButtonParallel_Click(object sender, EventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            await RunDownloadParallelAsync();

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            TextBoxResult.AppendLine($"Total execution time: {elapsedMs}");
        }
Beispiel #4
0
        private void ButtonNormal_Click(object sender, System.EventArgs e)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();

            RunDownloadSync();

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            TextBoxResult.AppendLine($"Total execution time: {elapsedMs}");
        }
Beispiel #5
0
        private async void ButtonParallel2_Click(object sender, EventArgs e)
        {
            Progress <ProgressReportModel> progress = new Progress <ProgressReportModel>();

            progress.ProgressChanged += ReportProgress;

            var watch = System.Diagnostics.Stopwatch.StartNew();

            var results = await RunDownloadParallelAsync2(progress);

            PrintResults(results);

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            TextBoxResult.AppendLine($"Total execution time: {elapsedMs}");
        }
Beispiel #6
0
 private void ReportWebsiteInfo(WebSiteDataModel data)
 {
     TextBoxResult.AppendLine($"{data.WebsiteUrl} downloaded: {data.WebsiteData.Length} characters long.");
 }
Beispiel #7
0
        private async Task ProcessCards(List <string> cards, IProgress <int> progress = null)
        {
            //int i = 1;

            using var semaphore = new SemaphoreSlim(500);

            var tasks = new List <Task <HttpResponseMessage> >();

            var tasksResolved = 0;

            tasks = cards.Select(async card =>
            {
                var json    = JsonConvert.SerializeObject(card);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                await semaphore.WaitAsync();

                try
                {
                    //TextBoxResult.Invoke(new Action(delegate
                    //{
                    //    if (i % 100 == 0)
                    //        TextBoxResult.AppendLine($"SemaphoreSlim : {i}");
                    //    i++;
                    //}));

                    var internalTask = await httpClient.PostAsync($"{apiURL}/cards", content);

                    if (progress != null)
                    {
                        tasksResolved++;
                        var percentage    = (double)tasksResolved / cards.Count;
                        percentage       *= 100;
                        var percentageInt = (int)Math.Round(percentage, 0);
                        progress.Report(percentageInt);
                    }

                    return(internalTask);
                }
                finally
                {
                    semaphore.Release();
                }
            }).ToList();

            var responses = await Task.WhenAll(tasks);

            var rejectedCards = new List <string>();

            foreach (var response in responses)
            {
                var content = await response.Content.ReadAsStringAsync();

                var responseCard = JsonConvert.DeserializeObject <CardResponse>(content);
                if (!responseCard.Approved)
                {
                    rejectedCards.Add(responseCard.Card);
                }
            }

            //int i = 1;
            //ProgressBarCards.Maximum = rejectedCards.Count;
            foreach (var card in rejectedCards)
            {
                TextBoxResult.AppendLine($"Card {card} was rejected");
                Thread.Sleep(1);
                //Application.DoEvents();
                //ProgressBarCards.Value = i;
                //i++;
            }


            //var tasks = new List<Task<HttpResponseMessage>>();
            //await Task.Run(() =>
            //{
            //    foreach (var card in cards)
            //    {
            //        var json = JsonConvert.SerializeObject(card);
            //        var content = new StringContent(json, Encoding.UTF8, "application/json");
            //        var responseTask = httpClient.PostAsync($"{apiURL}/cards", content);
            //        tasks.Add(responseTask);
            //    }
            //});
            //await Task.WhenAll(tasks);
        }