public main() { InitializeComponent(); searchedFiles = new List <string>(); ////第二种委托方式调用代码 searchComplete += new refreshListBoxHanlder(updateListBox); }
public ouyyn() { InitializeComponent(); searchedFiles = new List <string>(); searchComplete += new refreshListBoxHanlder(updateListBox); anotherform = new FileText(); }
private void updateListBox(object sender)//委托 { if (this.InvokeRequired) { refreshListBoxHanlder delegateFunc = new refreshListBoxHanlder(updateListBox); this.Invoke(delegateFunc, sender); } else { lst_showSpecifyFiles.Items.Clear(); lst_showSpecifyFiles.Items.AddRange(searchedFiles.ToArray()); } }
private void updateListBox(object sender) { FileList.Items.Clear(); if (this.InvokeRequired) { refreshListBoxHanlder delegateFunc = new refreshListBoxHanlder(updateListBox); this.Invoke(delegateFunc, sender); } else { FileList.Items.Clear(); FileList.Items.AddRange(searchedFiles.ToArray()); } MessageBox.Show("查询完毕!"); }
private void updateListBox(object sender) { //第一种委托方式调用代码 //lsbFiles.Items.Clear(); //lsbFiles.Items.AddRange(searchedFiles.ToArray()); //第二种委托方式调用代码 if (this.InvokeRequired) { refreshListBoxHanlder delegateFunc = new refreshListBoxHanlder(updateListBox); this.Invoke(delegateFunc, sender); } else { labfiles.Items.Clear(); labfiles.Items.AddRange(searchedFiles.ToArray()); } }
public static void searchFilesinFolder(object obj) //文件搜索线程调用的函数 { string strDir = obj.ToString(); main thisForm = (main)obj; strDir = thisForm.txDir.Text;//待搜索文件夹路径 if (strDir != null && strDir.Length > 0) { if (Directory.Exists(strDir)) { searchedFiles.Clear(); List <string> allSearcheFiles = new List <string>(); //用于存放所有搜索出来的文件 List <string> folders2Search = new List <string>(); //用于存放所有待搜索的文件夹 folders2Search.Add(strDir); while (folders2Search.Count > 0) { try { string curFolder = folders2Search[0]; folders2Search.RemoveAt(0); string[] files = Directory.GetFiles(curFolder); //搜索当前目录下所有文件 string[] dirs = Directory.GetDirectories(curFolder); //搜索当前目录下所有文件夹 foreach (string item in files) { allSearcheFiles.Add(item); } foreach (string item in dirs) { folders2Search.Add(item); } } catch { } } searchedFiles.AddRange(allSearcheFiles); //第一种传递方式:直接委托调用 refreshListBoxHanlder updateDelegate = new refreshListBoxHanlder(thisForm.updateListBox); thisForm.Invoke(updateDelegate, thisForm); //第二种传递方式:基于委托的消息机制 thisForm.searchComplete(thisForm); } } }
public static void searchFilesinFolder(object obj) { string strDir = obj.ToString(); FinalFileForm thisForm = (FinalFileForm)obj; strDir = thisForm.tbPath.Text; if (strDir != null && strDir.Length > 0) { if (Directory.Exists(strDir)) { searchedFiles.Clear(); List <string> allSearcheFiles = new List <string>(); List <string> folders2Search = new List <string>(); folders2Search.Add(strDir); while (folders2Search.Count > 0) { try { string curFolder = folders2Search[0]; folders2Search.RemoveAt(0); string[] files = Directory.GetFiles(curFolder); string[] dirs = Directory.GetDirectories(curFolder); foreach (string item in files) { allSearcheFiles.Add(item); } foreach (string item in dirs) { folders2Search.Add(item); } } catch { } } searchedFiles.AddRange(allSearcheFiles); refreshListBoxHanlder updateDelegate = new refreshListBoxHanlder(thisForm.updateListBox); thisForm.Invoke(updateDelegate, thisForm); } } }