Exemplo n.º 1
0
        private void SerializeChunk(object context)
        {
            SerializeChunkParms parms = (SerializeChunkParms)context;

            try {
                SerializeChunk(parms.Output, parms.ChunkNumber, parms.ChunkOffset, parms.ChunkSize, parms, parms.PreviousLock, parms.DeferredFormatter);
            } catch (Exception ex) {
                parms.Exceptions.Add(ex);
            }
        }
Exemplo n.º 2
0
        void IDeferredSerializable.FinishSerialization(Stream output, DeferredFormatter context)
        {
            bool useCompression = context.UseCompression;

            // formatVersion = 0 for GZIP, or 1 for uncompressed
            if (useCompression)
            {
                output.WriteByte(0);
            }
            else
            {
                output.WriteByte(1);
            }

            // chunkSize
            WriteUInt(output, serializationChunkSize);

            uint chunkCount = (uint)((this.length + (long)serializationChunkSize - 1) / (long)serializationChunkSize);

            PaintDotNet.Threading.ThreadPool threadPool = new PaintDotNet.Threading.ThreadPool(Processor.LogicalCpuCount);
            ArrayList    exceptions = ArrayList.Synchronized(new ArrayList(Processor.LogicalCpuCount));
            WaitCallback callback   = new WaitCallback(SerializeChunk);

            object previousLock = null;

            for (uint chunk = 0; chunk < chunkCount; ++chunk)
            {
                long chunkOffset          = (long)chunk * (long)serializationChunkSize;
                uint chunkSize            = Math.Min((uint)serializationChunkSize, (uint)(this.length - chunkOffset));
                SerializeChunkParms parms = new SerializeChunkParms(output, chunk, chunkOffset, chunkSize, previousLock, context, exceptions);
                threadPool.QueueUserWorkItem(callback, parms);
                previousLock = parms;
            }

            threadPool.Drain();
            output.Flush();

            if (exceptions.Count > 0)
            {
                throw new SerializationException("Exception thrown by worker thread", (Exception)exceptions[0]);
            }

            return;
        }
Exemplo n.º 3
0
        void IDeferredSerializable.FinishSerialization(Stream output, DeferredFormatter context)
        {
            bool useCompression = context.UseCompression;

            // formatVersion = 0 for GZIP, or 1 for uncompressed
            if (useCompression)
            {
                output.WriteByte(0);
            }
            else
            {
                output.WriteByte(1);
            }

            // chunkSize
            WriteUInt(output, serializationChunkSize);

            uint chunkCount = (uint)((this.length + (long)serializationChunkSize - 1) / (long)serializationChunkSize);

            PaintDotNet.Threading.ThreadPool threadPool = new PaintDotNet.Threading.ThreadPool(Processor.LogicalCpuCount);
            ArrayList exceptions = ArrayList.Synchronized(new ArrayList(Processor.LogicalCpuCount));
            WaitCallback callback = new WaitCallback(SerializeChunk);

            object previousLock = null;
            for (uint chunk = 0; chunk < chunkCount; ++chunk)
            {
                long chunkOffset = (long)chunk * (long)serializationChunkSize;
                uint chunkSize = Math.Min((uint)serializationChunkSize, (uint)(this.length - chunkOffset));
                SerializeChunkParms parms = new SerializeChunkParms(output, chunk, chunkOffset, chunkSize, previousLock, context, exceptions);
                threadPool.QueueUserWorkItem(callback, parms);
                previousLock = parms;
            }

            threadPool.Drain();
            output.Flush();

            if (exceptions.Count > 0)
            {
                throw new SerializationException("Exception thrown by worker thread", (Exception)exceptions[0]);
            }

            return;
        }