예제 #1
0
 public void StartTask()
 {
     try
     {
         block = streamManager.ReadBlock(block);
         if (ApplicationSettings.Compress)
         {
             block = Compressor.Compress(block);
         }
         else if (ApplicationSettings.Decompress)
         {
             block = Decompressor.Decompress(block);
         }
         streamManager.WriteBlock(block);
         block = null;
     }
     catch (Exception)
     {
         throw;
     }
 }
예제 #2
0
        private int RunProgram()
        {
            using (var reader = new FileStream(ApplicationSettings.InFile, FileMode.Open, FileAccess.Read))
            {
                using (var writer = new FileStream(ApplicationSettings.OutFile, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //Create input/output manager
                    StreamManager sm         = new StreamManager(reader, writer);
                    int           numOfTasks = 0;

                    //Get appropriate number of tasks that we'll nedd to run
                    if (ApplicationSettings.Compress)
                    {
                        numOfTasks = sm.GetNumberOfBlocksForCompression();
                    }
                    else if (ApplicationSettings.Decompress)
                    {
                        numOfTasks = sm.GetNumberOfBlocksForDecompression();
                    }

                    //Start the pool
                    ThreadPool tp = new ThreadPool(numOfTasks);
                    tp.Start();

                    //Create appropriate tasks and add them to the queue so that threads can pick tasks up
                    if (ApplicationSettings.Compress)
                    {
                        Compressor.CreateCompressionTasks(numOfTasks, tp, sm);
                    }
                    else if (ApplicationSettings.Decompress)
                    {
                        Decompressor.CreateDecompressionTasks(tp, sm);
                    }

                    //Play some animation while proccessing is happening
                    while (!tp.Finished && tp.Started)
                    {
                        Thread.Sleep(500);
                        Console.Write("\rProcessing \\ ");
                        Thread.Sleep(500);
                        Console.Write("\rProcessing | ");
                        Thread.Sleep(500);
                        Console.Write("\rProcessing / ");
                        Thread.Sleep(500);
                        Console.Write("\rProcessing | ");
                    }
                    Console.WriteLine();

                    //Add block sizes and mount of block to the end of the file
                    if (ApplicationSettings.Compress)
                    {
                        sm.WriteBlockSizes();
                    }

                    //Stop the pool
                    tp.Stop();

                    //Assert the results
                    if (tp.ExceptionHappened)
                    {
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
            }
        }