当前位置:软件学习 > 其它软件 >>

多线程复制下载文件参考代码

1   多线程下载文件原理

    其实就是把一个文件分成几个部分,不同线程(或者进程)负责下载文件的不同的部分。各种下载工具的底层原理应该和此相同,但由于是复杂的网络环境,实际会远比这复杂。

2 模拟代码实现。


[csharp]
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading; 
 
namespace ConsoleTest 

    class Program 
    { 
       
        private static int ThreadCount = 20; 
        static void Main(string[] args) 
        { 
            for (int i = 0; i < ThreadCount; i++) 
            { 
                Thread thread = new Thread(DoWork); 
                thread.Name = "Thread"+i; 
                Console.WriteLine("Thread" + i + "Start"); 
                thread.Start(i); 
            }           
            System.Console.ReadLine(); 
        } 
 
        public static void DoWork(object threadIndex) 
        { 
            Console.WriteLine(Thread.CurrentThread.Name); 
            string oldPath = @"F:\Reflector7.3.rar"; 
            string newPath = @"F:\Reflector7.rar"; 
            int fileLength = (int)(new FileInfo(oldPath).Length);//需要拷贝的文件大小 
            int lengthPerThread = (fileLength/ThreadCount + 1);//计算平均每个线程需要复制的文件内容大小 
            int startIndex = (int)threadIndex * lengthPerThread;//计算开始读取文件的位置 
 
            Console.WriteLine(Thread.CurrentThread.Name + "start write startIndex=" + startIndex); 
            int hasRead = 0; // 
            using (var oldFileReader = File.Open(oldPath, FileMode.Open, FileAccess.Read, FileShare.Read)) 
            { 
                oldFileReader.Seek(startIndex, SeekOrigin.Begin);//移动到需要读取的位置 
                byte[] array = new byte[100]; 
                using (var newFileWriter = File.Open(newPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) 
                { 
                    int read = oldFileReader.Read(array, 0, 100); 
                    newFileWriter.Seek(startIndex, SeekOrigin.Begin);//移动到需要写入的位置 
                    while (read > 0 && hasRead <= lengthPerThread)// 
                    { 
                        hasRead += read; 
                        newFileWriter.Write(array, 0, read); 
                        read = oldFileReader.Read(array, 0, 100); 
                        Random random = new Random(); 
                        int next= random.Next(1, 10); 
                        if(next==5)//生成随机数,制造多线程不按生成顺序结束 
                        { 
                            Console.WriteLine(Thread.CurrentThread.Name + "Sleep"); 
                            Thread.Sleep(50); 
                        } 
                    } 
                    
                } 
            } 
 
            Console.WriteLine(Thread.CurrentThread.Name +" end  write" + (startIndex + hasRead));       
        } 
    }   


摘自  xuexiaodong2009的专栏
补充:软件开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,