做了一个WinForm程序,自动检测是否插入了U盘等移动存储设备,若插入了就把硬盘文件下的文件复制到U盘上去,当插入多个U盘时,同时往多个U盘中写数据。现在只实现了往一个U盘上写数据,等待当前的U盘写完数据后再往下一个U盘中写数据,要同是往多个U盘中同时写数据,用多线程该怎么写?

解决方案 »

  1.   

    to  windily 
    -----------------------------
    我做的winForm是往多张mini SD卡上写复制数据,把硬盘上的某个文件夹下的文件复制到多个mini SD卡中,现在我只要换卡和取卡就可以了。。顺便公布一下写的多线程代码:
     #region Class  MyThread
        class MyThread
        {
            public static Dictionary<string, bool> status = new Dictionary<string, bool>();
            public static void CopyDirectory(object argu)
            {
                string Src = (argu as ThreadMethodHelper).Src;
                string Dst = (argu as ThreadMethodHelper).Dst;            if (status.ContainsKey(Dst))
                    status[Dst] = false;
                else
                    status.Add(Dst, false);            String[] Files;
                if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
                    Dst += Path.DirectorySeparatorChar;            if (!Directory.Exists(Dst))
                    Directory.CreateDirectory(Dst);            Files = Directory.GetFileSystemEntries(Src);
               
                foreach (string Element in Files)
                {
                    // Sub directories
                    if (Directory.Exists(Element))
                    {
                        //copyDirectory(Element, Dst + Path.GetFileName(Element));
                        ThreadMethodHelper args = new ThreadMethodHelper();
                        args.Src = Element;
                        args.Dst = Dst + Path.GetFileName(Element);
                        Thread my = new Thread(new ParameterizedThreadStart(CopyDirectory));
                        my.Start(args);
                        my.Join();                    
                    }
                    // Files in directory
                    else
                    {                    
                        File.Copy(Element, Dst + Path.GetFileName(Element), true);
                    }
                }
                status[Dst] = true;
            }
        }
        #endregion    class ThreadMethodHelper
        {
            public string Src;
            public string Dst;
          
        }
      

  2.   

    to  windily 
    -----------------------------
    我做的winForm是往多张mini SD卡上写复制数据,把硬盘上的某个文件夹下的文件复制到多个mini SD卡中,现在我只要换卡和取卡就可以了。。顺便公布一下写的多线程代码:
     #region Class  MyThread
        class MyThread
        {
            public static Dictionary<string, bool> status = new Dictionary<string, bool>();
            public static void CopyDirectory(object argu)
            {
                string Src = (argu as ThreadMethodHelper).Src;
                string Dst = (argu as ThreadMethodHelper).Dst;            if (status.ContainsKey(Dst))
                    status[Dst] = false;
                else
                    status.Add(Dst, false);            String[] Files;
                if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
                    Dst += Path.DirectorySeparatorChar;            if (!Directory.Exists(Dst))
                    Directory.CreateDirectory(Dst);            Files = Directory.GetFileSystemEntries(Src);
               
                foreach (string Element in Files)
                {
                    // Sub directories
                    if (Directory.Exists(Element))
                    {
                        //copyDirectory(Element, Dst + Path.GetFileName(Element));
                        ThreadMethodHelper args = new ThreadMethodHelper();
                        args.Src = Element;
                        args.Dst = Dst + Path.GetFileName(Element);
                        Thread my = new Thread(new ParameterizedThreadStart(CopyDirectory));
                        my.Start(args);
                        my.Join();                    
                    }
                    // Files in directory
                    else
                    {                    
                        File.Copy(Element, Dst + Path.GetFileName(Element), true);
                    }
                }
                status[Dst] = true;
            }
        }
        #endregion    class ThreadMethodHelper
        {
            public string Src;
            public string Dst;
          
        }
      

  3.   

            #region CheckDisk
            private void CheckDisk()
            { 
                char Char = this.textBox2.Text.Substring(0, 1).ToCharArray()[0];
                int Int = (int)Char;            
                for (int i = 0; i < 5; i++)
                {
                    char thePath = (char)(Int + i);
                    string path = thePath.ToString() + @":\";
                    if (Directory.Exists(path))
                    {                   
                        if (MyThread.status.ContainsKey(path))
                        {
                            if (MyThread.status[path])      //判断是否己拷贝文件
                            {
                                Exsits += path + "己准备\n";
                                result+= path + "己拷贝完成\n";
                            }
                            else
                            {
                                Exsits += path + "己准备\n";
                                ThreadMethodHelper argu = new ThreadMethodHelper();
                                result += path + "正在拷贝文件.....\n";
                                argu.Src = this.textBox1.Text;
                                argu.Dst = path;
                                Thread t = new Thread(new ParameterizedThreadStart(MyThread.CopyDirectory));
                                t.Start(argu);                                                      
                            }
                        }
                        else
                        {
                            //拷贝文件  
                            Exsits += path+"己准备\n";
                            ThreadMethodHelper argu = new ThreadMethodHelper();
                            result += path + "正在拷贝文件.....\n";
                            argu.Src = this.textBox1.Text;
                            argu.Dst = path;
                            Thread t = new Thread(new ParameterizedThreadStart(MyThread.CopyDirectory));
                            t.Start(argu);                                        
                        }
                    }
                    else
                    {
                        Exsits += path+"不存在\n";     //不存在盘符
                        MyThread.status.Remove(path);
                    }
                }
            }
            #endregion        #region timer1_Tick
            private void timer1_Tick(object sender, EventArgs e)
             {
                
                Exsits = string.Empty;
                result = string.Empty;
                Thread th = new Thread(CheckDisk);
                th.Start();
                th.Join();
                this.richTextBox1.Text = Exsits;
                this.richTextBox1.Refresh();
                this.richTextBox2.Text = result;
                this.richTextBox2.Refresh();
            }
            #endregion
      

  4.   

    我觉得你在CopyDirectory里面没必要再加线程了吧?
    my.Join(); 线程一直等待着,也没有提高性能啊,
    在 for   (int   i   =   0;   i   <   5;   i++) 
    个人认为循环里面加线程就可以了
      

  5.   

    刚测试了一下,的确如LZ所说的,多线程比单线程快.
    我的见解是,不对的请指出:因为多个线程抢占cpu时间片概率比一个线程抢占cpu时间片的概率大,所以分的是使用时间多一点.但是如果系统只有你这个程序,没有其他程序在执行(包括windows的进程),那肯定是单线程快.
    使用线程主要是因为它可以同时对不同的资源进行访问,而不是因为它同时对一个资源进行访问.