Beispiel #1
0
        private void UpdateProgressBar(ByteArgs e, int step, int totalsteps)
        {
            if (progressbar.InvokeRequired)
            {
                UpdateProgressBarCallback d = UpdateProgressBar;
                progressbar.Invoke(d, new object[] { e, step, totalsteps });
            }
            else
            {
                int   stepsize = (int)Math.Round((float)progressbar.Maximum / totalsteps);
                float ratio    = (float)e.Downloaded / e.Total;
                int   val      = (int)Math.Floor(stepsize * step + stepsize * ratio);

                if (val <= progressbar.Maximum)
                {
                    progressbar.Value = val;
                    TaskbarProgress.SetValue(this.Handle, progressbar.Value, progressbar.Maximum);
                }
                progressbar.Refresh();
                Invalidate();
            }
        }
Beispiel #2
0
        public static MemoryStream DownloadWebFile(string url)
        {
            //open a data stream from the supplied URL
            WebRequest  webReq = WebRequest.Create(url);
            WebResponse webResponse;

            try
            {
                webResponse = webReq.GetResponse();
            }
            catch (Exception e)
            {
                MainForm.ErrorDescription = "Failed to retrieve remote revision info...\n" + e.Message;
                return(null);
            }
            Stream dataStream = webResponse.GetResponseStream();

            //Download the data in chuncks
            byte[] dataBuffer = new byte[1024];

            //Get the total size of the download
            int dataLength = (int)webResponse.ContentLength;

            //lets declare our downloaded bytes event args
            ByteArgs byteArgs = new ByteArgs();

            byteArgs.Downloaded = 0;
            byteArgs.Total      = dataLength;

            //we need to test for a null as if an event is not consumed we will get an exception
            if (BytesDownloaded != null)
            {
                BytesDownloaded(byteArgs);
            }

            //Download the data
            MemoryStream memoryStream = new MemoryStream();

            while (!MainForm.AppClosing)
            {
                //Let's try and read the data
                int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);
                if (bytesFromStream == 0)
                {
                    byteArgs.Downloaded = dataLength;
                    byteArgs.Total      = dataLength;
                    if (BytesDownloaded != null)
                    {
                        BytesDownloaded(byteArgs);
                    }

                    //Download complete
                    break;
                }
                else
                {
                    //Write the downloaded data
                    memoryStream.Write(dataBuffer, 0, bytesFromStream);

                    byteArgs.Downloaded += bytesFromStream;
                    byteArgs.Total       = dataLength;
                    if (BytesDownloaded != null)
                    {
                        BytesDownloaded(byteArgs);
                    }
                }
            }

            //Release resources
            dataStream.Close();

            // Rewind and return the stream
            memoryStream.Position = 0;
            return(MainForm.AppClosing ? null : memoryStream);
        }
Beispiel #3
0
 private void WebdataOnBytesDownloaded(ByteArgs ba)
 {
     UpdateProgressBar(ba, 0, 2);
 }