使用唯一的类对象多线程池C#

问题描述 投票:0回答:1

这是对Use ThreadPool in C# within a loop and wait for all threads to finishSave the content of several csv files into a searchable array C#的跟进问题>

我有一个看起来像这样的代码:

using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;

    namespace ThreadPooling
    {
        class Program
        {
            static void Main(string[] args)
            {


                var FolderNames= new List<(string name)>()
                {
                  "folder1",
                  "folder2"
                }

                var tasks = new List<Task>();
                foreach (var folder in FolderNames)
                {
                    Console.WriteLine("Staring process" + folder.name + "...");
                    var task = Task.Run(() => Job(folder.name));
                    tasks.Add(task);
                }

                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All calculations done.");
                Console.WriteLine("\nPress any key to exit the program...");
                Console.ReadKey();

            }

            public class Job()
            {
               public Job(folder)
               {
                   CsvFile File = new CsvFile();
                   File.OpenCsvFiles(folder); //opens all csv files within the folder
               }
            }

    public class CsvFile
    {
        string folder;

        static Dictionary<string, Dictionary<int, Dictionary<int, string>>> _dictionary =
                                new Dictionary<string, Dictionary<int, Dictionary<int, string>>>();

        public void OpenCsvFiles(string folder) //opens all the csv files in the output folder and saves their's content to a Dictionary
        {
            this.folder = folder;

            foreach (string path in Directory.GetFiles(folder, "*.csv")) // fileName is the file name WITH its full path
            {
                try
                {
                    string[][] m_Data = File
                    .ReadLines(path)
                    .Where(line => !string.IsNullOrWhiteSpace(line))
                    //.Skip(1) //skipping the headers
                    .Select(line => line.Split(','))
                    .ToArray();

                    PopulateDictionary(Path.GetFileName(path), m_Data); //fills the Dictionary with each file
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error in OpenCsvFiles (" + path + ") : " + ex.Message);
                }
            }
        }

        static void PopulateDictionary(string filename, string[][] data)
        {
            _dictionary.Add(filename, new Dictionary<int, Dictionary<int, string>>());

            for (int i = 0; i < data.Length; i++)
            {
                _dictionary[filename].Add(i, new Dictionary<int, string>());

                for (int j = 0; j < data[i].Length; j++)
                {
                    _dictionary[filename][i].Add(j, data[i][j]);
                }
            }
        }



        }
    }
}

[我想做的是循环显示文件夹列表,打开每个文件夹中的所有.csv文件,并将值保存到CsvFile中的字典,每个并行运行的Process均可访问该字典。但是,我遇到的问题是,与C ++不同,C#似乎没有在CsvFile中创建单独的Process类对象。也就是说,如果我在folder1和folder2中遇到一个具有相同文件名的文件,例如文件Alpha.csv,则会收到一个错误,提示代码不愿向字典添加两次相同的键Alpha。

但是我希望CsvFile中的字典与Process的每个类对象一起创建和使用,而不是一个CsvFile对象被创建然后由所有Process类并行使用。这可能吗?

这是在循环中在C#中使用ThreadPool并等待所有线程完成并将几个csv文件的内容保存到可搜索数组C#中的后续问题,我有一个看起来像是的代码...

c# multithreading class
1个回答
0
投票
这是因为在.net中,引用类型(例如object类,例如string)作为引用传递,而值类型(intfloatchar等)作为值传递。
© www.soinside.com 2019 - 2024. All rights reserved.