Example #1
0
 protected virtual void OnDownProgressChanged(DownProgressChangedEventArgs e)
 {
     if (DownProgressChangedEvent != null)
     {
         DownProgressChangedEvent(this, e);
     }
 }
Example #2
0
        void downProgressChanged(object sender, DownProgressChangedEventArgs e)
        {
            SetpbTaskPercentage(e.Percentage);
            string strPercent = "";
            string strUnit    = "KB";
            int    unitVal    = 1024;

            DateTime now      = DateTime.Now;
            TimeSpan interval = now - lastUpdate;

            if (interval.Milliseconds > 500 || e.Percentage == 100)
            {
                double timeDiff = interval.TotalSeconds;
                double sizeDiff = e.bytesIn - lastbytes;
                double speed    = Math.Floor((double)(sizeDiff) / timeDiff);
                lastbytes  = e.bytesIn;
                lastUpdate = now;
                setLbInfoText(Msg.DEF_DOWNLOADING);

                if (e.progressMaxValue > (1000 * 1000))
                {
                    strUnit = "MB";
                    unitVal = 1024 * 1024;
                }
                strPercent = (Math.Round(Math.Round(e.bytesIn, 1) / unitVal)) + strUnit + "/" +
                             (Math.Round(Math.Round(e.progressMaxValue, 0) / unitVal)) + strUnit;
                double tempSpeed = Math.Round(Math.Round(speed, 2) / (1024));
                if (tempSpeed > 1020)
                {
                    SetLbPercentageText(strPercent + " (" + (Math.Round(Math.Round(speed, 2) / (1024 * 1024))) + " MB/s)");
                }
                else
                {
                    SetLbPercentageText(strPercent + " (" + (Math.Round(Math.Round(speed, 2) / (1024))) + " KB/s)");
                }
                setMainFormText("( " + e.Percentage + "% ) " + Msg.DEF_TITLE);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="webResponse">WebResponse</param>
        /// <param name="path">0 - 절대경로, 1 - 상대경로, 2 - 파일이름</param>
        public void receiveData(WebResponse webResponse, string[] path, ref DownProgressChangedEventHandler DownProgressChangedEvent, ref DownProgressCompletedEventHandler DownProgressCompletedEvent)

        {
            FileStream fileStream = null;
            string     destZipDir = "";

            try
            {
                string winTempDir = System.IO.Path.GetTempPath();
                Debug.WriteLine("Window Temp Directory : " + winTempDir);
                //string destZipDir = path[0] + "\\" +path[1] + "\\";
                destZipDir = winTempDir + "\\" + path[1] + "\\";

                Directory.CreateDirectory(Path.GetDirectoryName(destZipDir));

                byte[] fromBuff = new byte[256];
                fileStream = new FileStream(destZipDir + "\\" + path[2], FileMode.OpenOrCreate, FileAccess.Write);
                int    count            = 0;
                double progressMaxValue = double.Parse(webResponse.ContentLength.ToString());
                Stream readStream       = webResponse.GetResponseStream();

                //            webResponse.GetResponseStream()

                /*
                 * while ((read = bis.read(b)) != -1) {
                 *          outStream.write(b, 0, read);
                 *      }
                 */

                //while ((count = webResponse.GetResponseStream().Read(fromBuff, 0, fromBuff.Length)) > 0)
                while ((count = readStream.Read(fromBuff, 0, fromBuff.Length)) > 0)
                {
                    //progressValue += fromBuff.Length;
                    fileStream.Write(fromBuff, 0, count);

                    //double bytesIn = double.Parse(progressValue.ToString());
                    double bytesIn = double.Parse(fileStream.Length.ToString());
                    //tempSize = double.Parse(e.TotalBytesToReceive.ToString());

                    double percentage = (bytesIn / progressMaxValue) * 100;



                    //ValidateEndEvent(this, e);
                    DownProgressChangedEventArgs vEe = new DownProgressChangedEventArgs(int.Parse(Math.Truncate(percentage > 100 ? 100 : percentage).ToString()), bytesIn, progressMaxValue);
                    if (DownProgressChangedEvent != null)
                    {
                        DownProgressChangedEvent(this, vEe);
                    }
                    //DownProgressChangedEvent(this,vEe);
                    //.ValidateEndEvent();//.ValidateEnd(vEe);
                    //bg.ReportProgress(int.Parse(Math.Truncate(percentage > 100 ? 100 : percentage).ToString()), bytesIn);
                }
                DownProgressCompletedEventArgs dCe = new DownProgressCompletedEventArgs(false, "");//(int.Parse(Math.Truncate(percentage > 100 ? 100 : percentage).ToString()), bytesIn);
                if (DownProgressCompletedEvent != null)
                {
                    DownProgressCompletedEvent(this, dCe);
                }
            }
            catch (System.Threading.ThreadAbortException e)
            {
                Debug.WriteLine(e.ToString());
                try
                {
                    //DirectoryInfo dirInfo = new DirectoryInfo(targetPath + "\\" + pathDir);
                    DirectoryInfo dirInfo = new DirectoryInfo(destZipDir);

                    if (dirInfo.Exists)
                    {
                        FileInfo[] sfiles = dirInfo.GetFiles();

                        foreach (FileInfo fi in sfiles)
                        {
                            fi.Delete();
                        }
                        dirInfo.Delete();
                    }
                }
                catch (Exception IOe)
                {
                    Debug.WriteLine("an exception has occured in extracting : " + IOe.ToString());
                }
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (webResponse != null)
                {
                    webResponse.Close();
                }
            }
        }