比如 
Thread t = null;
            for (int i = 1; i <= 5; i++)
            {
                t = new Thread(searchthead);//searchthead为查找文件的方法 返回字符串数组
                
            t.Start();
            }
但这样用,会查找出5个同样的文件出来,怎样当一个线程查找过的文件,另一个线程就不去查了?

解决方案 »

  1.   

    一个list<file>,在searchthead中查到的文件添加到里面,完成后从list<>里面读取
      

  2.   

    ZengHD ,我查找的是同一文件夹
      

  3.   

    建个hashtable 找到个就存,hashtable重复的会存不进
      

  4.   

    1:你的文件有多少?如果不是上W的话,无需使用多线程。
    2:先建立一个文件List,根据算法把不同的序号区段分配给不同的线程。
    比如:有1-10000个文件,那么分配
    1-2000给线程1。
      

  5.   

    lude8880  这样做是可以,但如果是这样,不是每次查找的时候还要从全局变量里拉出这个文件名做个判断,那这样效率?
      

  6.   

    文件在通一个文件夹下
    那就以那个文件夹作为初始路径
    先找里面的子文件(夹)
    然后对每个子文件夹在启动一个新的线程
    如果不分子文件夹,而是所有文件都放在一个文件夹下,那就没有什么好办法了楼上说的定义一个list,将已经找到的文件放到list中,之后的线程查找list中是否存在同名文件这种做法效率太低,而且每个线程做的都是重复工作,没有什么意义
      

  7.   

    我觉的也没有必要做多线程。。你可是尝试下WINDOWS里面同时对2个盘区查找,和一次查找2个盘区。。看下哪个速度更快
      

  8.   

    多线程写起来比较复杂啊,我把代码贴到这里给你吧。不知道你能不能看明白;有点长,我使用它来搜索文件的注释狠详细
    --------------------------frmSerach.cs---------------------------
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.Collections;
    using System.Text.RegularExpressions;
    using System.IO;namespace MyUdisk
    {
        public partial class frmSerach : Form
        {
            private CThreadSearch m_objThreadC;
            public static bool Initial_flag;
            public static frmStart frmtmp;
            int filecount;//记录搜索到文件的个数;
            public frmSerach()
            {
                InitializeComponent();
                m_objThreadC = new CThreadSearch(this, new SearchFileEventHandler(m_objThreadC_SearchEvent));
                Initial_flag = false;
                filecount = 0;
                listViewFiles.DoubleClick += new EventHandler(listViewFiles_DoubleClick);
                this.cmntrv_Path.TreeView.ImageList = this.imglist;
                
            }        void listViewFiles_DoubleClick(object sender, EventArgs e)
            {
                if(listViewFiles.Items.Count>0)
                { Clipboard.Clear();
                    Clipboard.SetText(listViewFiles.Items[0].SubItems[3].Text.Trim());
                if (MessageBox.Show("提示:你所选文件的完整路径已经复制到剪贴板上,\n关闭本窗体请点击是,重新选择请点击否", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {      this.Dispose();
                }  }  }
     //事件处理函数
            void m_objThreadC_SearchEvent(CThreadSearch sender, CSearchFileEventArgs args)
            {
                ProcessEvent(m_lblC, args);
            }
          
            //处理CThreadSearch对象传回来的事件对象
            void ProcessEvent(Label lbl , CSearchFileEventArgs args)
            {
                switch (args.EventType)
                {
                    case SearchFileEventTypes.Start:
                        lbl.Text = "查找线程启动...";
                        break;
                    case SearchFileEventTypes.Stop:
                        lbl.Text = "查找线程停止.";
                        break;
                    case SearchFileEventTypes.ProcessFile:
                        lbl.Text = "当前位置:"+args.FilePath;
                        break;
                    case SearchFileEventTypes.MatchedFile:
                        lbl.Text = "搜索到文件:"+args.FilePath;
                      //如果匹配则调用增加函 m_lstFiles.Items.Add(args.FilePath);
                        this.additemtolistview(args.FilePath);
                        filecount =filecount +1;
                        break;
                    case SearchFileEventTypes.Error:
                        lbl.Text = "错误:" + args.FilePath;
                        break;
                }
            }
            private void additemtolistview(string filepath)
            {
                m_lbCount.Text =" 共搜索到"+ this.filecount.ToString()+"条记录";
             
                FileInfo fsi = new FileInfo(filepath);
               
        
                listViewFiles.Items.Add(fsi.Name);
                listViewFiles.Items[listViewFiles.Items.Count - 1].SubItems.Add(fsi.Length.ToString());
                listViewFiles.Items[listViewFiles.Items.Count - 1].SubItems.Add(fsi.CreationTime.ToString());
                listViewFiles.Items[listViewFiles.Items.Count - 1].SubItems.Add(fsi.LastWriteTime.ToString());
                listViewFiles.Items[listViewFiles.Items.Count - 1].SubItems.Add(fsi.FullName);
     
            }
            //开始查找
            private void m_btnStart_Click(object sender, EventArgs e)
            {
                string sPattern;
                string path;
                sPattern = "";            if (cmb_Pattern.Text.Length > 5)
                {
                    sPattern = cmb_Pattern.Text.Substring(cmb_Pattern.Text.Length - 5, 4);
                  
                }
                else if(cmb_Pattern.Text.Length>0)
                {
                    string str = cmb_Pattern.Text.Trim();
                    sPattern = str;
                }
                if (sPattern.Length <= 0)
                {     MessageBox.Show("请输入查询符");
                    return;        }
             
                else
                {      path = cmntrv_Path.Text.Trim();
          m_objThreadC.StartSearch(path, sPattern);  }
                 }
            //停止查找
            private void m_btnStopC_Click(object sender, EventArgs e)
            {    m_objThreadC.Stop();
            }
        
            private void Form1_Load(object sender, EventArgs e)
            {
                cmb_Pattern.SelectedIndex = 0;
            }
        }
      

  9.   

    续上    public class CThreadSearch
        {
            private bool m_bStart = false;
            private Queue<string> m_queFolders;
            private Regex m_objRegex;
            private Form m_objForm;
            private Delegate m_objInvoke;
           
            
            public CThreadSearch(Form frm , Delegate objInvoke)
            {
                m_objForm = frm;
                m_objInvoke = objInvoke;
                m_queFolders = new Queue<string>();
            }
            //启动查找
            public void StartSearch(string sRootFolder,string sFindPattern)
            {
                m_queFolders.Clear();
                m_queFolders.Enqueue(sRootFolder);
                m_objRegex = new Regex(sFindPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                //创建用来查找的线程
                Thread objThread = new Thread(new ThreadStart(fnThread));
                objThread.IsBackground = true;
                //设置启动标志
                m_bStart = true;
                //启动线程
                objThread.Start();
            }
            //通知对象,停止查找
            public void Stop()
            {
                m_bStart = false;
              
            }
            //查找线程
            private void fnThread()
            {
                //检查参数
                if (m_queFolders == null || m_objRegex == null) return;
                //事件通知,查找开始
                OnEvent("" , SearchFileEventTypes.Start);
                while (m_bStart && m_queFolders.Count > 0)
                {
                    string sFolder = m_queFolders.Dequeue();
                    try
                    {
                        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(sFolder);
                        OnEvent(sFolder, SearchFileEventTypes.ProcessFile);
                        //子目录,加入队列
                        DirectoryInfo[] arrSubDirs = dir.GetDirectories();
                        for (int i = 0; i < arrSubDirs.Length; i++)
                        {
                            m_queFolders.Enqueue(arrSubDirs[i].FullName);
                            if (!m_bStart) break;
                        }
                        //检查是否需要终止
                        if (!m_bStart) break;
                        //检查文件是否匹配
                        FileInfo[] arrFiles = dir.GetFiles();
                        for (int i = 0; i < arrFiles.Length; i++)
                        {
                            if (m_objRegex.IsMatch(arrFiles[i].Extension.ToUpper()))
                            {
                                //找到匹配的文件
                                OnEvent(arrFiles[i].FullName, SearchFileEventTypes.MatchedFile);
                            }
                            else
                            {
                                //不匹配, 只是通知处理过该文件
                                OnEvent(arrFiles[i].FullName, SearchFileEventTypes.ProcessFile);
                            }
                            if (!m_bStart) break;
                        }
                        if (!m_bStart) break;
                    }
                    catch (Exception)
                    {
                        //发生错误
                        OnEvent(sFolder, SearchFileEventTypes.Error);
                    }
                }
                //事件通知,查找结束
                OnEvent("", SearchFileEventTypes.Stop);
            }
            //用来触发事件
            private void OnEvent(string sFilePath , SearchFileEventTypes eType)
            {
                if (m_objForm != null && m_objInvoke != null)
                {
                    CSearchFileEventArgs args = new CSearchFileEventArgs(eType , sFilePath);
                    m_objForm.Invoke(m_objInvoke, new object[] { this, args });
                }
            }
        }
        //用来定义事件函数的委托
        public delegate void SearchFileEventHandler(CThreadSearch sender, CSearchFileEventArgs args);
        //事件的类型
        public enum SearchFileEventTypes
        {
            Start,          //开始查找
            ProcessFile,    //检查指定的文件
            MatchedFile,    //找到匹配的文件
            Stop   ,          //查找结束
            Error           //发生错误
        }
        //事件参数
        public class CSearchFileEventArgs : EventArgs
        {
            string m_sFilePath;
            SearchFileEventTypes m_eType;
            public string FilePath
            {
                get { return m_sFilePath; }
            }
            public SearchFileEventTypes EventType
            {
                get { return m_eType; }
            }
            public CSearchFileEventArgs(SearchFileEventTypes eType, string sFile)
            {
                m_eType = eType;
                m_sFilePath = sFile;
            }
        }
    }
      

  10.   

    牛贴的哥们,朋友,兄弟姐妹们帮我踩踩这个贴啊,急~~~~~~~~~晚上通宵搞顶的任务-.-!!
    http://topic.csdn.net/u/20080911/22/8b02ddc9-0a3c-497e-8a2d-7a1735bc116a.html
      

  11.   

    注意上文中有个控件是自定义的,你用的时候要换掉的就是cmntrv_Path。如果需要源码请email我
    或者去我的资源下载
      

  12.   

    先建立一个文件List,根据算法把不同的序号区段分配给不同的线程。 
    比如:有1-10000个文件,那么分配 
    1-2000给线程1。