0

我有 400 个批处理文件,我想同步运行 10 个批处理文件,并在前 10 个完成后运行第二个 10 个文件。

我正在使用 c#,并且之前使用过 process.start()。

有什么建议么?

4

2 回答 2

1

使用自 .NET 4.0 起可用的任务并行库 (TPL)(任务对象)。

不完全是您想要做的,但这里是从 MSDN 获取的示例:http: //msdn.microsoft.com/en-us/library/dd537609.aspx

public class Example
{
   public static void Main()
   {
      // Create the task object by using an Action(Of Object) to pass in the loop 
      // counter. This produces an unexpected result.
      Task[] taskArray = new Task[10];
      for (int i = 0; i < taskArray.Length; i++) {
         taskArray[i] = Task.Factory.StartNew( (Object obj) => {
                                                 var data = new CustomData() {Name = i, CreationTime = DateTime.Now.Ticks}; 
                                                 data.ThreadNum = Thread.CurrentThread.ManagedThreadId;
                                                 Console.WriteLine("Task #{0} created at {1} on thread #{2}.",
                                                                   data.Name, data.CreationTime, data.ThreadNum);
                                               },
                                              i );
      }
      Task.WaitAll(taskArray);     
   }
}
于 2013-10-22T20:22:00.057 回答
0

听起来您需要使用信号量类。您可以使用 Semaphore 来限制可以访问您的文件的线程数。

例如:

    private static int _fileProcessed;
    private static Semaphore _pool;
    static void Main(string[] args)
    {
        // let say I have a list of file names in an array
        string[] files = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "z" };

        // we only want 10 threads working at one time
        _fileProcessed = 0;
        _pool = new Semaphore(10, 10);

        foreach (string file in files)
        {
            _pool.WaitOne();    // wait until I have a free resource 

            Thread t = new Thread(() => ProcessFile(file));
            t.Start();
        }

        // you can either wait here until are theads are done
        // or exit
        // In the example I want to wait
        while (_fileProcessed < files.Length)
        {
            Thread.Sleep(500);
        }

        Console.WriteLine("We are done!");
        //Console.ReadLine();
    }


    private static void ProcessFile(string fileName)
    {
        int id = System.Threading.Thread.CurrentThread.ManagedThreadId;

        Console.WriteLine("{0} - {1:HH:mm:ss.fff} - Working on File: {2}", id, DateTime.Now, fileName);

        // Do Work ....

        // to simulate work getting done - get a number of seconds
        // between 10 & 30 seconds
        Thread.Sleep(GetWorkLength());

        Console.WriteLine("{0} - {1:HH:mm:ss.fff} - Worked Completed on file: {2}", id, DateTime.Now, fileName);

        Interlocked.Add(ref _fileProcessed, 1);         
        _pool.Release();
    }

    // Random work length
    private static Random _workLength = new Random();
    private static object _lock = new object();
    private static int GetWorkLength()
    {
        lock (_lock)
        {
            return _workLength.Next(10, 30) * 1000;
        }
    }

这是输出: 示例代码的输出

于 2013-10-22T20:18:23.160 回答