读书人

c#如何获取多线程的下载速度

发布时间: 2012-04-07 17:31:52 作者: rapoo

c#怎么获取多线程的下载速度。

C# code
 System.IO.Stream ns;            FileStream fs;            ns = null;            byte[] nbytes;//接收缓冲区            int nreadsize;//接收字节数            ns = null;            nbytes = new byte[512];            nreadsize = 0;            //("线程" + taskIndex.ToString() + "开始接收");-----------------------------------------关注每个任务线程开始            fs = new FileStream(httpLoadFile.TaskFileList[taskIndex], System.IO.FileMode.Create);            //try            //{                request = (HttpWebRequest)WebRequest.Create( httpLoadFile.SourceUrl );                //接收的起始位置及接收的长度                 request.AddRange( httpLoadFile.TaskStartList[taskIndex] , httpLoadFile.TaskStartList[taskIndex] + httpLoadFile.TaskSizeList[taskIndex] );                ns = request.GetResponse( ).GetResponseStream( );//获得接收流                nreadsize = ns.Read( nbytes , 0 , 512 );                while ( nreadsize > 0 )                {                    fs.Write( nbytes , 0 , nreadsize );                    nreadsize = ns.Read( nbytes , 0 , 512 );                    //("线程" + taskIndex.ToString() + "正在接收");----------------------------------关注每个任务线程进行                }                fs.Close( );                ns.Close( );


[解决办法]
你的线程中应该有个计时器,记录总时间,和接收到的总字节数,然后做一个除法
[解决办法]
探讨

C# code
怎么转换成cs思想。。。让b/s结构的我好悲催。。。。。。。。路过的都给点意见了。连在这问了

[解决办法]
探讨

引用:

每个线程在一段时间内下载的总字节数之和就是总速度。

timer就可以,定时统计每个线程的下载速度,求和
大侠啊。。。如果我再wpf里面调用的话没有timer控件 只能写个隔一秒调用的东西。。那我该怎么即时呢

[解决办法]
各线程更新到一个对象中去进行统计
[解决办法]
我不是很清楚下载速度的计算方法。

不过可以针对每个线程创建一个各个线程接收总字节数的数组,每个线程接收的字节数都加到数组的相应位置。比如第一个线程接收字节的总数是数组的第一个元素。
然后定义一个DispatcherTimer,每隔1秒中去计算上面那个数组中各个线程接收的字节总数。

如果从最开始计计算,到现在的时刻。得出
下载速度=sum(readBytes)/计时器走过的时间

如果从上一秒计算,到现在的时刻。得出
下载速度=sum(readBytes)-上一秒的字节总和

好像第二种更准确一些,仅供参考。

C# code
        private double[] readBytes = new double[5];        private double oldsum = 0;        private long timegones = 0;        private Random ran = new Random();        private SimulateSpeed()        {            DispatcherTimer timer = new DispatcherTimer();            timer.Tick += new EventHandler(timer_Tick);            timer.Interval = TimeSpan.FromSeconds(1);            timer.Start();                       //这里用了多个线程,最好用ThreadPool            Thread thread1 = new Thread(new ParameterizedThreadStart(Process));            thread1.Start(0);            Thread thread2 = new Thread(new ParameterizedThreadStart(Process));            thread2.Start(1);            Thread thread3 = new Thread(new ParameterizedThreadStart(Process));            thread3.Start(2);            Thread thread4 = new Thread(new ParameterizedThreadStart(Process));            thread4.Start(3);            Thread thread5 = new Thread(new ParameterizedThreadStart(Process));            thread5.Start(4);        }        void timer_Tick(object sender, EventArgs e)        {            timegones++;  //计时器走过的时间            double sum = readBytes.Sum();            //double speed=sum/timegones;  //下载速度=sum(readBytes)/计时器走过的时间            double speed = sum -oldsum;    //下载速度=sum(readBytes)-上一秒的字节总和            oldsum = sum;            textshow.Text = string.Format("当前速度:{0}kb/s", Math.Round(speed,2).ToString());        }        void Process(object obj)        {            int i= int.Parse(obj.ToString());            while (true)            {                readBytes[i] += ran.NextDouble();                Thread.Sleep(10);            }        } 


[解决办法]
计时用 System.Diagnostics.Stopwatch
定时用 System.Threading.Timer

读书人网 >C#

热点推荐