Beispiel #1
0
        void FinishWebRequest(IAsyncResult result)
        {
            HttpWebRequestWithFile request  = result.AsyncState as HttpWebRequestWithFile;
            HttpWebResponse        response = request.Request.EndGetResponse(result) as HttpWebResponse;

            if (response.StatusCode == HttpStatusCode.OK)
            {
                using (Stream fd = File.Create(request.FileName))
                    using (Stream responseStream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        byte[] buffer = new byte[1024];
                        int    nRead;
                        while ((nRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fd.Write(buffer, 0, nRead);
                        }
                        //Invoke(new EventHandle{progressBar.Value++;});

                        Thread.Sleep(request.Delay);
                        progressBar.Invoke(new Action(() => progressBar.Value++));
                        if (progressBar.Value == progressBar.Maximum)
                        {
                            Thread.Sleep(1000);
                            FinishLoading();
                        }
                    }
            }
            else
            {
                MessageBox.Show("Nu s-a putut incarca populatia", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #2
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                requestPopulation                = new HttpWebRequestWithFile();
                requestPopulation.Request        = (HttpWebRequest)WebRequest.Create("https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/demo_pjan.tsv.gz");
                requestPopulation.Request.Accept = "*/*";
                requestPopulation.Request.Host   = "ec.europa.eu";
                requestPopulation.FileName       = "population.txt";
                requestPopulation.Delay          = 1000;
                requestPopulation.Request.BeginGetResponse(new AsyncCallback(FinishWebRequest), requestPopulation);

                requestMortality                = new HttpWebRequestWithFile();
                requestMortality.Request        = (HttpWebRequest)WebRequest.Create("https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/demo_magec.tsv.gz");
                requestMortality.Request.Accept = "*/*";
                requestMortality.Request.Host   = "ec.europa.eu";
                requestMortality.FileName       = "mortality.txt";
                requestMortality.Delay          = 1500;
                requestMortality.Request.BeginGetResponse(new AsyncCallback(FinishWebRequest), requestMortality);

                requestNewBorns                = new HttpWebRequestWithFile();
                requestNewBorns.Request        = (HttpWebRequest)WebRequest.Create("https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/demo_fasec.tsv.gz");
                requestNewBorns.Request.Accept = "*/*";
                requestNewBorns.Request.Host   = "ec.europa.eu";
                requestNewBorns.FileName       = "newBorns.txt";
                requestNewBorns.Delay          = 2000;
                requestNewBorns.Request.BeginGetResponse(new AsyncCallback(FinishWebRequest), requestNewBorns);
            }

            catch (Exception ex)
            {
                try
                {
                    FinishLoading();
                }
                catch (Exception ex1)
                {
                }
                this.Invoke(new Action(() => MessageBox.Show("Nu s-a putut realiza conexiunea cu baza de date EUROSTAT!", "Atentionare", MessageBoxButtons.OK, MessageBoxIcon.Warning)));
            }
        }