public override void Save()
        {
            byte[] buffer = null;
            try
            {
                using (var targetFileStream = File.Create(TargetFilePath))
                {
                    var orderNumber = 0;
                    while (!StatusManager.ProcessIsCanceled && StatusManager.Exception == null)
                    {
                        if (StatusManager.AllDecompressIsCompleted && DictionaryWritingManager.IsEmpty())
                        {
                            StatusManager.SavingToFileIsCompleted = true;
                            break;
                        }

                        var isSuccess = DictionaryWritingManager.TryRemove(orderNumber, out buffer);
                        if (!isSuccess)
                        {
                            Thread.Sleep(ThreadTimeout);
                            continue;
                        }

                        targetFileStream.Write(buffer, 0, buffer.Length);
                        orderNumber++;
                    }
                }
            }
            catch (Exception ex)
            {
                StatusManager.Exception = ex;
            }
        }
Ejemplo n.º 2
0
        public override void Start(int threadNumber)
        {
            var threadCompressedDataManager = CompressedDataManagers[threadNumber];

            threadCompressedDataManager.WaitOne();

            Interlocked.Increment(ref UnsyncThreads);
            BytesBlock bytesBlock = null;

            while (!StatusManager.ProcessIsCanceled && StatusManager.Exception == null)
            {
                if (StatusManager.ReadingIsCompleted && threadCompressedDataManager.IsEmpty())
                {
                    Interlocked.Decrement(ref UnsyncThreads);
                    if (UnsyncThreads == 0)
                    {
                        StatusManager.AllDecompressIsCompleted = true;
                    }
                    break;
                }

                var isSuccess = threadCompressedDataManager.TryDequeue(out bytesBlock);
                if (!isSuccess)
                {
                    threadCompressedDataManager.WaitOne();
                    continue;
                }

                var buffer = BytesCompressUtil.DecompressBytes(bytesBlock.Buffer);

                DictionaryWritingManager.Add(bytesBlock.OrderNumber, buffer);
            }
        }
        public override void Save()
        {
            var orderNumber = 0;

            byte[] bytesBlock = null;

            try
            {
                using (var targetFileStream = File.Create(TargetFilePath))
                {
                    while (!StatusManager.ProcessIsCanceled && StatusManager.Exception == null)
                    {
                        if (StatusManager.AllCompressIsCompleted && DictionaryWritingManager.IsEmpty())
                        {
                            StatusManager.SavingToFileIsCompleted = true;
                            break;
                        }

                        var isSuccess = DictionaryWritingManager.TryRemove(orderNumber, out bytesBlock);
                        if (!isSuccess)
                        {
                            Thread.Sleep(ThreadTimeout);
                            continue;
                        }

                        var blockLengthInBytes = BitConverter.GetBytes(bytesBlock.Length);
                        targetFileStream.Write(blockLengthInBytes, 0, blockLengthInBytes.Length);
                        targetFileStream.Write(bytesBlock, 0, bytesBlock.Length);

                        orderNumber++;
                    }
                }
            }
            catch (Exception ex)
            {
                StatusManager.Exception = ex;
            }
        }