C#多线程处理
创建多线程,并带参数!
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; namespace ThreadProcess { class Program { static void Main(string[] args) { string path = @"c:\test\"; string[] Dirs = Directory.GetFiles(path, "*.txt"); //创建相应线程数 ArrayList al = new ArrayList(); foreach(string s in Dirs) { if (!File.Exists(s.Substring(0, s.LastIndexOf('.')) + ".lock")) al.Add(s); } Thread[] t = new Thread[al.Count]; for (int i = 0; i <al.Count; i++) { t[i] = new Thread(new ParameterizedThreadStart(Process)); t[i].Start(Dirs[i].ToString()); } } static void Process(object fileName) { string procFile = (string)fileName; string lockFile = procFile.Substring(0, procFile.LastIndexOf('.')) + ".lock"; //if (File.Exists(lockFile)) //{ // return; //} //else //{ // FileStream fs = new FileStream(lockFile, FileMode.CreateNew); // fs.Close(); //} FileStream fs = new FileStream(lockFile, FileMode.CreateNew); fs.Close(); while (true) { //这里作相应处理工作 Console.Write(lockFile); Thread.Sleep(50); } File.Delete(lockFile); File.Delete(procFile); } } }