首页 > 解决方案 > 并行压缩文件夹,c#中的最佳解决方案

问题描述

我需要在不同的档案中同时压缩更多的文件夹以更快地进行。我尝试使用一个主要的后台工作人员,并从这里我做了一个 for 循环来为每个文件夹启动其他后台工作人员。我注意到后台工作人员同时启动但不能一起工作什么是做并行存档的最佳方法?你建议什么?创建存档我使用 zipforge 库

DoWork of the main backgroundworker
if (Directory.Exists(destination))
        {
            if (MessageBox.Show("", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
            {
                goto EXIT_UTENTE;
            }
            else
            {
                bool finish = false;
                int j = 1;

                string dest_appoggio = destination;

                do
                {
                    dest_appoggio = destination + "_" + j;

                    if (Directory.Exists(dest_appoggio))
                    {
                        j++;
                    }
                    else
                    {
                        destination = dest_appoggio;
                        dataprogressiva = data + "_" + j;
                        finish = true;
                    }
                }
                while (finish == false);
            }
        }

        destinationdefault = destination;

        var row_list1 = GetDataGridRows(dgwRobot);

        int riga = -1;

        foreach (DataGridRow single_row1 in row_list1)
        {
            bool rowselected = false;
            single_row1.Dispatcher.Invoke(new Action(() => { rowselected = single_row1.IsSelected; }));


            if (rowselected == true)
            {
                riga = single_row1.GetIndex();
                var robotProcessed = false;

                while (!robotProcessed)
                {
                    for (var threadNum = 0; threadNum < MaxBackupContemporanei; threadNum++)
                    {
                        if (!threadArray[threadNum].IsBusy)
                        {
                            threadArray[threadNum].RunWorkerAsync(riga);

                            robotProcessed = true;

                            break;
                        }
                    }
                }
            }
            Thread.Sleep(500);
        }

        bool bwTerminati = false;

        while (!bwTerminati)
        {
            for (var threadNum = 0; threadNum < MaxBackupContemporanei; threadNum++)
            {
                if (threadArray[threadNum].IsBusy)
                {
                    bwTerminati = false;
                    threadNum = MaxBackupContemporanei;
                }
                else
                {
                    bwTerminati = true;
                }
            }
        }

标签: c#

解决方案


推荐阅读