Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                ArgsAnalyzer.InitialValidation(args);
                switch (args[0])
                {
                case "compress":
                    var compressor = new Compressor(args[1], args[2]);
                    compressor.Start();
                    break;

                case "decompress":
                    var decompressor = new Decompressor(args[1], args[2]);
                    decompressor.Start();
                    break;

                default:
                    Console.WriteLine("The first argument should be \"compress\" or \"decompress\"!");
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\nPlease fix it and try again");
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            for (ushort i = 0; i < _compressors.Length; i++)
            {
                var compressor = new Compressor(i, _pool, _writer);
                _compressors[i] = compressor;

                var thread = new Thread(() => compressor.Start());
                thread.IsBackground   = true;
                _compressorThreads[i] = thread;
                thread.Start();
            }

            while (true)
            {
                Job job = null;
                SpinWait.SpinUntil(() => _stop || _reader.TryTake(out job));
                if (_stop)
                {
                    break;
                }

                ConcurrentQueue <Job> compressor = null;
                SpinWait.SpinUntil(() => _stop || _pool.TryDequeue(out compressor));
                if (_stop)
                {
                    break;
                }

                compressor.Enqueue(job);
            }

            foreach (var compressor in _compressors)
            {
                compressor.Stop();
            }
            foreach (var thread in _compressorThreads)
            {
                thread.Join();
            }

            Console.WriteLine("Compressor pool terminated");
            if (_success)
            {
                Console.WriteLine("\nSuccess!\n");
            }
            else
            {
                Console.WriteLine("\nFailed!\n");
            }
        }