Пример #1
0
        static void Main(string[] args)
        {
            /*switch (args[0])
             * {
             *  case "compress":
             *      Compressor.Compress(args[1], args[2]);
             *      break;
             *  case "decompress":
             *      Compressor.Decompress(args[1], args[2]);
             *      break;
             *  default:
             *      Console.WriteLine("incorrect command");
             *      break;
             * }*/
            //это пока для простоты
            //Compressor.Compress("source.mkv", "out");
            CompressManager.Compress("source.mkv", "out");
            Console.WriteLine("Done");
            Console.ReadLine();

            /*Queue<int> queue = new Queue<int>();
             * SyncEvents syncEvents = new SyncEvents();
             *
             * Console.WriteLine("Configuring worker threads...");
             * Producer producer = new Producer(queue, syncEvents);
             * Consumer consumer = new Consumer(queue, syncEvents);
             * Thread producerThread = new Thread(producer.ThreadRun);
             * Thread[] consumerThread = new Thread[2];
             * for (int i = 0; i < 2; i++)
             * {
             *  consumerThread[i] = new Thread(consumer.ThreadRun);
             *  consumerThread[i].Name = "Consumer" + i;
             * }
             *
             * Console.WriteLine("Launching producer and consumer threads...");
             * producerThread.Start();
             * for(int i=0;i<2;i++)
             *  consumerThread[i].Start();
             *
             * for (int i = 0; i < 1; i++)
             * {
             *  Thread.Sleep(2500);
             *  ThreadSyncSample.ShowQueueContents(queue);
             * }
             *
             * Console.ReadLine();
             * producerThread.Join();
             * for(int i=0;i<2;i++)
             * consumerThread[i].Join();*/
        }
Пример #2
0
        public void ThreadRun()
        {
            try
            {
                if (File.Exists(destination))
                {
                    File.Delete(destination);
                }
                using (FileStream resFile = new FileStream(destination + ".gz", FileMode.Create, FileAccess.Write))
                {
                    Task task         = null;
                    int  currPartNumb = 0;

                    while ((CompressManager.IsAnyConsumerThreadAlive()) || (doneTasks.Count > 0))
                    {
                        lock (((ICollection)doneTasks).SyncRoot)
                            if (doneTasks.Any(p => p.numberOfPart == currPartNumb))
                            {
                                task = doneTasks.Find(p => (p != null) && (p.numberOfPart == currPartNumb));
                                doneTasks.Remove(task);
                            }
                        if (task != null)
                        {
                            resFile.Write(task.buffer, 0, task.buffer.Length);
                            Console.WriteLine("Writed " + currPartNumb);
                            task = null;
                            currPartNumb++;
                        }
                    }

                    /*while ((eventIndex = WaitHandle.WaitAny(syncEvents.EventTaskArray)) != -1)
                     * {
                     *  if (eventIndex == 1)
                     *  {
                     *      countThreadFinished++;
                     *      //(syncEvents.EventTaskArray[1] as ManualResetEvent).Reset();//нужно ли это делать?
                     *  }
                     *  lock (((ICollection)doneTasks).SyncRoot)
                     *  {
                     *      //если очередь выполненных заданий пуста и все сжимающие потоки завершились, завершаем работу
                     *      if ((doneTasks.Count == 0) && (countThreadFinished == CompressManager.MaxTasksCount))
                     *          return;
                     *
                     *      //достаем следующий кусок
                     *      if (doneTasks.Any(p => p.numberOfPart == currPartNumb))
                     *      {
                     *          task = doneTasks.Find(p => (p != null) && (p.numberOfPart == currPartNumb));
                     *          doneTasks.Remove(task);
                     *      }
                     *  }
                     *  if (task != null)
                     *  {
                     *      resFile.Write(task.buffer, 0, task.buffer.Length);
                     *      Console.WriteLine("Writed " + currPartNumb);
                     *      currPartNumb++;
                     *  }
                     * }*/
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }