首页 > 代码库 > Parallel线程使用

Parallel线程使用

public void run(Action<List<string>> onl oad)
        {
            List<string> directoryLists = new List<string>();
            directoryLists = Directory.GetDirectories(m_importPath).ToList();          
            Parallel.ForEach(directoryLists,new ParallelOptions { MaxDegreeOfParallelism=10}, i =>
            {
                List<string> files = importer(i);

                onl oad(files);

           }

     }

 

Action<List<string>> onl oad = dir =>
            {
                BeginInvoke(new EventHandler((obj, even) =>
                {
                    var root = new TreeNode(dir[0]);
                    for (int i = 1; i < dir.Count; i++)
                    {
                        string fileName = dir[i].Substring(dir[i].LastIndexOf(‘\\‘) + 1);
                        root.Nodes.Add(fileName.Remove(fileName.IndexOf(‘.‘)));
                    }
                    this.treeView1.Nodes.Add(root);
                }), null);

            };

import.run(onload);