比如
Thread t = null;
for (int i = 1; i <= 5; i++)
{
t = new Thread(searchthead);//searchthead为查找文件的方法 返回字符串数组
t.Start();
}
但这样用,会查找出5个同样的文件出来,怎样当一个线程查找过的文件,另一个线程就不去查了?
Thread t = null;
for (int i = 1; i <= 5; i++)
{
t = new Thread(searchthead);//searchthead为查找文件的方法 返回字符串数组
t.Start();
}
但这样用,会查找出5个同样的文件出来,怎样当一个线程查找过的文件,另一个线程就不去查了?
2:先建立一个文件List,根据算法把不同的序号区段分配给不同的线程。
比如:有1-10000个文件,那么分配
1-2000给线程1。
那就以那个文件夹作为初始路径
先找里面的子文件(夹)
然后对每个子文件夹在启动一个新的线程
如果不分子文件夹,而是所有文件都放在一个文件夹下,那就没有什么好办法了楼上说的定义一个list,将已经找到的文件放到list中,之后的线程查找list中是否存在同名文件这种做法效率太低,而且每个线程做的都是重复工作,没有什么意义
--------------------------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;
}
}
{
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;
}
}
}
http://topic.csdn.net/u/20080911/22/8b02ddc9-0a3c-497e-8a2d-7a1735bc116a.html
或者去我的资源下载
比如:有1-10000个文件,那么分配
1-2000给线程1。