利用不同的thread來完成,顯示進度列的方式,其範例程式如下所示:

在xaml中建立如下元件

DemoWebDownload 

<code>

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.IO;

using System.Net;

using System.Threading;

 

namespace DemoWebFDownload

{

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window

    {

        private double _TOTALSIZE = 0;

 

        public Window1()

        {

            InitializeComponent();

            progressBar1.Value = 0;

            _TOTALSIZE = 0;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            if (textBox1.Text == "xxx.zip")

            {

                ThreadStart ts = new ThreadStart(Test);


                Thread t = new Thread(ts);

                t.Start();

            }

 

        }

 

        

        public void Test()

        {

            double MAXSIZE=0;

            string url = "http://xxx/xxx/xxx.zip";

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

 

            Console.WriteLine("總大小:"+httpResponse.ContentLength);

            MAXSIZE=(int)httpResponse.ContentLength;

            System.IO.Stream dataStream = httpResponse.GetResponseStream();

            byte[] buffer = new byte[8192];

 

            FileStream fs = new FileStream("C:\\xxx.zip",

                FileMode.Create, FileAccess.Write);

            int size = 0;

            

            do

            {

                size = dataStream.Read(buffer, 0, buffer.Length);

                _TOTALSIZE += size;

                Console.WriteLine("目前下載大小:" + _TOTALSIZE);

                Console.WriteLine("總大小:" + MAXSIZE);

                Console.WriteLine("目前百分比:" + _TOTALSIZE / MAXSIZE);

                double svalue = _TOTALSIZE / MAXSIZE * 100;

                showvalue(svalue);

                //pb.Value = _TOTALSIZE / MAXSIZE * 100;

                Console.WriteLine("value:" + svalue);

                if (size > 0)

                    fs.Write(buffer, 0, size);

            } while (size > 0);

            fs.Close();

 

            httpResponse.Close();

 

            Console.WriteLine("Done at " + DateTime.Now.ToString("HH:mm:ss.fff"));

        }

        delegate void SetEventCallback(double value);

        public void showvalue(double value)

        {

            if (this.Dispatcher.Thread == Thread.CurrentThread)

            {

                progressBar1.Value = value;

            }

            else

            {

                SetEventCallback d = new SetEventCallback(showvalue);

                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, d, value);

            }

        }

    }

}

 

</code>

arrow
arrow
    全站熱搜

    狼翔月影 發表在 痞客邦 留言(0) 人氣()