zoukankan      html  css  js  c++  java
  • 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:	est";
                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);
    
            }
        }
    }
  • 相关阅读:
    178
    177
    176
    175
    To Do List
    洛谷 P4198 楼房重建
    斯特林数
    容斥原理
    组合数学笔记
    激光相机数据融合(6)--激光相机标定
  • 原文地址:https://www.cnblogs.com/gisext/p/6405196.html
Copyright © 2011-2022 走看看