Dict.CN 在线词典, 英语学习, 在线翻译

伊牛娃

知其然而不知其所以然,乃飘飘然
posts - 16, comments - 12, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

多线程的输出问题

Posted on 2007-12-14 11:54 伊牛娃 阅读(94) 评论(0) 编辑 收藏

今天写了一个多线程,但是输出的时候有一个不明白的地方
本来输出的先后顺序应该如下所示

但是输出后变成了下图所示


可是我用Thread.Sleep(1000);输出顺序又正确了

代码如下

 

 static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"F:\wwwroot\eBayName\eBayCategory Two\Jewelry & Watches");

            FileInfo[] fi = di.GetFiles();
            int length = fi.Length;

            for (int i = 0;i< length; i++)
            {
                ParameterizedThreadStart para = new ParameterizedThreadStart(IsReg);            
                Thread trd = new Thread(para);              
                string txtFileName = fi[i].FullName;
                //Thread.Sleep(1000);
                trd.Start(txtFileName);              
            }
            Console.ReadLine();
}
static void IsReg(object name)
        {          
            Console.WriteLine("TXT文件名: "+name);//输出文件名
        }