Multithreading task to process files in c#

后端 未结 4 1850
你的背包
你的背包 2021-01-18 12:07

I\'ve been reading a lot about threading but can\'t figure out how to find a solution to my issue. First let me introduce the problem. I have files which need to be processe

4条回答
  •  礼貌的吻别
    2021-01-18 12:57

    I would start by organizing your data structure a bit better. Having two separate arrays not only increases data duplication, but also creates implicit coupling which may not be obvious to the person looking at your code.

    A class which would hold information about a single task might look something like:

    public class TaskInfo
    {
        private readonly string _hostName;
        public string HostName
        {
            get { return _hostName; }
        }
    
        private readonly ReadOnlyCollection _files;
        public ReadOnlyCollection Files
        {
            get { return _files; }
        }
    
        public TaskInfo(string host, IEnumerable files)
        {
            _hostName = host;
            _files = new ReadOnlyCollection(files.ToList());
        }
    }
    

    Creating a list of tasks is now much more straightforward:

    var list = new List()
    {
        new TaskInfo(
            host: "host1",
            files: new[] { @"c:\host1\file1.txt", @"c:\host1\file2.txt" }),
    
        new TaskInfo(
            host: "host2",
            files: new[] { @"c:\host2\file1.txt", @"c:\host2\file2.txt" })
    
        /* ... */
    };
    

    And now that you have your tasks ready, you can simply use various classes from the System.Threading.Tasks namespace to invoke them in parallel. If you really want to limit the number of concurrent tasks, you can simply use the MaxDegreeOfParallelism property:

    Parallel.ForEach(
        list, 
        new ParallelOptions() { MaxDegreeOfParallelism = 10 },
        taskInfo => Process(taskInfo)
    );
    

    If you wanted to create your own pool of threads, you could have also achieved a similar thing using a ConcurrentQueue with multiple consumer threads, possibly waiting on a list of WaitHandles to know when they're done.

提交回复
热议问题