使用Parallel.ForEachAsync怎么高效实现动态数组的遍历功能啊?

public class Scan
    {
private Form1 frm;
        /// <summary>
        /// 扫描基础目录
        /// </summary>
        private string base_dir;
        /// <summary>
        /// 线程最大总数
        /// </summary>
        private int thread_num;

        public Scan(Form1 form,string baseDir="",int threadNum=3)
        {
            this.frm = form;
            this.base_dir = baseDir;
            this.thread_num = threadNum;
        }
public void RunTask()
        {
            Task.Run(async () =>
            {
                #region 第1次初始化数据
                Random ran = new Random();
                int RandKey = ran.Next(0, 100);
                int[][] dirsArray = this.GetWaitScanOfDirsArrayFromDataBase(RandKey);
                #endregion
                while (dirsArray.Length > 0)
                {
                    //设置线程数
                    if (dirsArray.Length < this.thread_num)
                    {
                        this.thread_num = dirsArray.Length;
                    }
                    ParallelOptions parallelOptions = new ParallelOptions()
                    {
                        MaxDegreeOfParallelism = this.thread_num
                    };

                    Task result = Parallel.ForEachAsync(dirsArray, parallelOptions, async (currentTypeArray, _) =>
                    {
                        Debug.WriteLine($"CurrentThreadId={Thread.CurrentThread.ManagedThreadId},执行{string.Join(", ", currentTypeArray)}");
                        this.frm.Func(string.Join(", ", currentTypeArray));
                        await Task.Delay(1);
                    });

                    while (result.IsCompleted)
                    {
                        Debug.WriteLine("是否完成:{0}", result.IsCompleted);
                        //Debug.WriteLine("最低迭代:{0}", result.LowestBreakIteration);                    
                        RandKey = ran.Next(0, 100);
                        dirsArray = this.GetWaitScanOfDirsArrayFromDataBase(RandKey);
                        break;
                    }
                    Debug.WriteLine("本次应循环的数:{0}", RandKey);
                }

            });
        }

private DirAttr[] GetWaitScanOfDirsArrayFromDataBase()
        {
            return null;
        }


        private int[][] GetWaitScanOfDirsArrayFromDataBase(int count)
        {
            int[][] dirsArray = new int[count][];
            for (int i = 0; i < count; i++)
            {
                int[] innerArray = new int[i];
                for (int j = 0; j < i; j++)
                {
                    innerArray[j] = i;
                }
                dirsArray[i] = innerArray;
            }
            return dirsArray;
        }
}

目前dirsArray是固定的数组,但我想把dirsArray设置成动态数组,不断向dirsArray传入值,就不能直接传入到Parallel.ForEachAsync执行,我只有等Parallel.ForEachAsync全部执行完了,再给dirsArray赋新值进去继续执行,我总是觉得这个方法还不是太高效的方法,请大神们帮忙看看还有没有更高效的方法来执行动态的数组数据啊?
以下是调用的方法:

private void button2_Click(object sender, EventArgs e)
        {
             Scan s=new Scan(this);
            s.RunTask();
        }
阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
Microsoft
子站问答
访问
宣传栏