首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求助:关于C#中线程传递参数的有关问题,

2011-12-22 
求助:关于C#中线程传递参数的问题,高手请进!我想做一个遍历文件夹所有文件的一个程序,现在遇到以下问题:1、

求助:关于C#中线程传递参数的问题,高手请进!
我想做一个遍历文件夹所有文件的一个程序,现在遇到以下问题:
1、我不用线程的时候,如果文件夹和文件少,则不会问题。但是如果是文件多了就会死机。
2、我如果用线程,参数又是一个问题。
希望有高手能帮我解决问题,谢谢。   附代码.
                public   static   void   GetAllFiles(string   parentDir)
                {
                        string[]   dir   =   Directory.GetDirectories(parentDir);
                        for   (int   i   =   0;   i   <   dir.Length;   i++)
                                GetAllFiles(dir[i]);
                        string[]   file   =   Directory.GetFiles(parentDir);
                        for   (int   i   =   0;   i   <   file.Length;   i++)
                        {

                                //toolStripStatusLabel1.Text   =   file[i];
                                string   file_type   =   Path.GetExtension(file[i]);
                                if   (file_type   ==   ".htm ")
                                {
                                        All_file   +=   file[i].ToString()   +   "| ";

                                }
                        }
                }


                private   void   button2_Click(object   sender,   EventArgs   e)
                {
                        All_file   =   " ";
                        if   (filepath   !=   null)
                        {
                                button1.Enabled   =   false;
                                button2.Enabled   =   false;
                                listBox1.Items.Clear()   ;
                                toolStripStatusLabel1.Text   =   "统计文件中... ";
                                progressBar1.Value   =   0;


                                //Thread   InstanceCaller   =   new   Thread(new   ThreadStart(work.GetAllFiles));
                                //InstanceCaller.Start();
                                GetAllFiles(filepath);
                                string[]   arr_files   =   All_file.Substring(0,   All_file.Length   -   1).Split( '| ');
                                progressBar1.Maximum   =   arr_files.Length;
                                //MessageBox.Show(arr_files.Length.ToString());
                                for   (int   s   =   0;   s   <   arr_files.Length;   s++)
                                {
                                        progressBar1.Value   =   s+1;
                                        toolStripStatusLabel1.Text   =   arr_files[s];
                                        listBox1.Items.Add(arr_files[s]);
                                          clear(arr_files[s]);


 
                                }

                                //this.demoThread   =   new   Thread(new   ThreadStart(this.Thread1));
                                //this.demoThread.Start();
                                //toolStripStatusLabel1.Text   =   "完成,共操作 "   +   sum   +   "个文件。 ";
                                button1.Enabled   =   true;
                                button2.Enabled   =   true;
                               
                        }
                        else  
                        {
                                MessageBox.Show( "请选择要操作的目录! ");


                        }
                }




[解决办法]
把public static void GetAllFiles(string parentDir)和参数封装在一个类中,不知道行不
public class YourClass{
private string parentDir;
public string ParentDir{
get{return parentDir;}
}
public void GetAllFiles(){
//.............
}
}返回数据的话用回调方法
[解决办法]
线程传递参数 如果在2.0中可以如下
Thread th1 = new Thread(new ParameterizedThreadStart(work.GetAllFiles));
th1.Start(@ "d:\test ");


在1.1需要写一个类来辅助传参

看楼主的代码线程方法似乎已经是类方法 那么你只需要在实例化该类时,将路径参数一并初始化即可
[解决办法]
public class Worker
{
private string dir;
public Worker(string dir)
{
this.dir = dir;
}
}

public void GetAllFiles()
{
string[] files = Directory.GetFiles(dir, "*.* ",SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{

//toolStripStatusLabel1.Text = files[i];
string file_type = Path.GetExtension(file[i]);
if (file_type == ".htm ")
{
All_file += file[i].ToString() + "| ";

}
}
}
[解决办法]
可以通过ParameterizedThreadStart给线程入口函数传递参数。如:

Thread t=new Thread(ParameterizedThreadStart(test));
t.Start( "teststr ");

void test(object o)
{
string s=(string)o;
Console.WriteLine(s);
}

输出:teststr

热点排行