Multithreading task to process files in c#

后端 未结 4 1847
你的背包
你的背包 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:32

    I think ThreadPool is the perfect solution for you. It will handle the threads by itself and queue their work. Moreover, you can set the maximum threads limit and it will still queue your work even if you have more than the maximum number of threads.

    ThreadPool.SetMaxThreads([YourMaxThreads],[YourMaxThreads]);
    
    foreach (var t in host_thread)
    {
        ThreadPool.QueueUserWorkItem(Foo, t);
    }
    


    private static void Foo(object thread)
    {
        foreach (var file in (thread as host_file_thread).group_file_paths)
        {
            (thread as host_file_thread).process_file(file);
        }
    }
    

    Although I would suggest you change your data structure and keep the process_file method from it

提交回复
热议问题