Beispiel #1
0
        private static void ManageThreads(string inputName, string outputName, ArchAction act, int bufferSize, uint workersCount = 0)
        {
            General.Log($"{act} started.");
            Archiver.act = act;

            if (bufferSize > defaultChunkSize)
            {
                ArchWorker.ChunkSize = bufferSize;
            }

            using (FileStream outFileStream = File.Create(outputName))
            {
                ArchWorker.OutFileStream = outFileStream;

                using FileStream inFileStream = File.OpenRead(inputName);

                if (workersCount < 1)
                {
                    workersCount = ChooseWorkersCount();
                }

                ArchWorker[] archivers = CreateWorkers(workersCount);

                int idx = 0;

                do
                {
                    ArchWorker arch = archivers[idx];

                    General.Log($"main thread about to Wait for arch {idx} done, to start it in new thread again");
                    arch.Done.WaitOne();

                    //Load the next portion to be processed and written
                    int read = act == ArchAction.Zip ? arch.ReadChunk(inFileStream) : arch.ReadZippedChunk(inFileStream);

                    if (read > 0)
                    {
                        //TODO
                        //Thread th = new Thread(new ParameterizedThreadStart(act == ArchAction.Zip ? ZipWork : UnzipWork));

                        Thread th;
                        if (act == ArchAction.Zip)
                        {
                            //th = new Thread(new ParameterizedThreadStart(ZipWork));
                            th = new Thread(ZipWork);
                        }
                        else
                        {
                            //th = new Thread(new ParameterizedThreadStart(UnzipWork));
                            th = new Thread(UnzipWork);
                        }

                        th.Start(arch);

                        if (++idx >= archivers.Length)
                        {
                            idx = 0;
                        }
                    }
                    else
                    {
                        //have read all

                        if (archivers.Length == 1)
                        {
                            break;
                        }

                        if (--idx < 0)
                        {
                            idx = archivers.Length - 1;
                        }

                        General.Log($"main thread about to Wait for the last arch {idx} done...");
                        archivers[idx].Done.WaitOne(); //prev is finished, meaning all before prev are finished

                        //now safe to go out and dispose outputStream
                        break;
                    }
                } while (true);

                //archivers = null;//no need
            }

            General.Log($"{act} finished.");
        }