/// <summary>
        /// Runs the NoEnzyme protein digestion on the GPU
        /// </summary>
        /// <param name="Proteins"></param>
        /// <param name="listOfQueries"></param>
        /// <returns></returns>
        public IEnumerable <Tuple <Peptide, int> > DigestProteomeOnTheFlyNoEnzyme(List <Protein> Proteins, Queries listOfQueries)
        {
            IsDone     = false;
            CumulMatch = new System.Collections.Concurrent.ConcurrentQueue <Tuple <Peptide, int> >();

            Task.Factory.StartNew(() =>
            {
                RunGPUProteinDigest(options, Proteins, listOfQueries);
                IsDone = true;
            });

            Tuple <Peptide, int> item;

            while (!IsDone)
            {
                if (CumulMatch.TryDequeue(out item))
                {
                    yield return(item);
                }
            }

            while (CumulMatch.TryDequeue(out item))
            {
                yield return(item);
            }
        }
Exemple #2
0
        protected void OnAdd(object obj, NotifierEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.DisplayRecord.character) && e.DisplayRecord.character.Equals("engage") && CanShow(e.DisplayRecord))
            {
                if (mostRecentQueue.Count >= 10)
                {
                    VmosoTileDisplayRecord recentDisplayRecord;
                    mostRecentQueue.TryDequeue(out recentDisplayRecord);
                }

                mostRecentQueue.Enqueue(e.DisplayRecord);
            }

            if (currentNotifier == null)
            {
                VmosoNotifierTile ui = new VmosoNotifierTile(top, e.DisplayRecord, expectedSize, Session);
                currentNotifier    = ui;
                ui.OnShownStatus  += new EventHandler(OnShownHandler);
                ui.OnHiddenStatus += new EventHandler(OnHiddenStatus);
                VmosoTileDisplayRecord immediateDisplayRecord;
                notifierQueue.TryDequeue(out immediateDisplayRecord);
                notifierCounter++;
                ui.ShowMessage();
            }
        }
        private static IEnumerable <T> TakeQueuedItems()
        {
            T obj;

            while (m_queue.TryDequeue(out obj))
            {
                yield return(obj);
            }
        }
Exemple #4
0
        static void clear_queue()
        {
            string s;

            while (!_incoming_data.IsEmpty)
            {
                _incoming_data.TryDequeue(out s);
            }
        }
        int GetID()
        {
            var id = -1;

            if (Ids.TryDequeue(out id))
            {
                return(id);
            }
            return(-1);
        }
Exemple #6
0
 public static void ConsoleWriteLine(string _Message, ConsoleColor _Color = ConsoleColor.White, bool _LogToFile = true)
 {
     if (sm_ExternalLog_ConsoleWriteLine != null)
     {
         try
         {
             sm_ExternalLog_ConsoleWriteLine(_Message, _Color);
         }
         catch (Exception)
         { }
     }
     else
     {
         Console.ForegroundColor = _Color;
         string fullMessage = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " : " + _Message;
         Console.WriteLine(fullMessage.Substring("yyyy/MM/dd ".Length));
         if (_LogToFile == true)
         {
             try
             {
                 var logFile = System.IO.File.Open("Log.txt", System.IO.FileMode.Append, System.IO.FileAccess.Write);
                 if (logFile.Length > 1 * 1024 * 1024)
                 {
                     //Om loggfilen är större än 1MB, skapa en ny och gör gamla till backup
                     logFile.Close();
                     System.IO.File.Copy("Log.txt", "Log_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".txt");
                     string queuedLog = "";
                     while (sm_QueuedExtraLogs.TryDequeue(out queuedLog) == true)
                     {
                         fullMessage = queuedLog + "\r\n" + fullMessage;
                     }
                     System.IO.File.WriteAllText("Log.txt", fullMessage + "\r\n");
                 }
                 else
                 {
                     System.IO.StreamWriter fw = new System.IO.StreamWriter(logFile);
                     string queuedLog          = "";
                     while (sm_QueuedExtraLogs.TryDequeue(out queuedLog) == true)
                     {
                         fw.WriteLine(queuedLog);
                     }
                     fw.WriteLine(fullMessage);
                     fw.Flush();
                     logFile.Close();
                 }
             }
             catch (Exception)
             {
                 sm_QueuedExtraLogs.Enqueue(fullMessage);
             }
         }
         Console.ResetColor();
     }
 }
Exemple #7
0
        public IBuffer Pop()
        {
            IBuffer item;

            if (!mPool.TryDequeue(out item))
            {
                item = CreateBuffer();
            }
            item.Reset();
            return(item);
        }
        private void ClearConnectionInformation()
        {
            IgsRequest throwaway;

            while (_outgoingRequests.TryDequeue(out throwaway))
            {
            }
            _incomingMovesAreForThisGame = null;
            _gamesBeingObserved.Clear();
            this.GamesYouHaveOpened.Clear();
            // _gamesInProgressOnIgs.Clear();
        }
Exemple #9
0
        public void Stop()
        {
            _WriterStop();
            _ReaderStop();
            RequestPackage  val;
            ResponsePackage val2;

            while (_Sends.TryDequeue(out val) ||  _Receives.TryDequeue(out val2))
            {
            }
            Singleton <Log> .Instance.WriteInfo("Agent online leave.");
        }
Exemple #10
0
        public void Enqueue(T i_Frame)
        {
            lock (m_LockObject)
            {
                m_Q.Enqueue(i_Frame);
                T overflow;

                while (m_Q.Count > Limit && m_Q.TryDequeue(out overflow))
                {
                    // the work is done in the condition
                }
            }
        }
        private void Update()
        {
            Func <IEnumerator> action;

#if UNITY_WSA && !UNITY_EDITOR
            while (updateQueue.TryDequeue(out action))
            {
#else
            while (updateQueue.Count > 0)
            {
                lock (updateQueue)
                {
                    action = updateQueue.Dequeue();
                }
#endif
                action();
            }

            Func <IEnumerator> co;
#if UNITY_WSA && !UNITY_EDITOR
            while (coRoutineQueue.TryDequeue(out co))
            {
#else
            while (coRoutineQueue.Count > 0)
            {
                lock (coRoutineQueue)
                {
                    co = coRoutineQueue.Dequeue();
                }
#endif
                StartCoroutine(co());
            }
        }
Exemple #12
0
        public override ArraySegment <byte> GetBlodk(int offset, int size, out int newOffset)
        {
            int length = Length - offset;

            if (length >= size)
            {
                length = size;
            }
            newOffset = offset + length;
            byte[] buffer = null;
            if (!mPool.TryDequeue(out buffer))
            {
                buffer = new byte[size];
            }
            System.IO.Stream fsstream;
            if (InnerResource)
            {
                fsstream = Assembly.GetManifestResourceStream(FullName);
            }
            else
            {
                fsstream = System.IO.File.OpenRead(FullName);
            }
            using (fsstream)
            {
                fsstream.Seek(offset, SeekOrigin.Begin);
                int len = fsstream.Read(buffer, 0, buffer.Length);
                return(new ArraySegment <byte>(buffer, 0, len));
            }
        }
        private void timerForPolling_Tick(object sender, EventArgs e)
        {
            DateTime start = DateTime.Now;

            while (!updating && FrameSets.Count > 0)
            {
                try
                {
                    string path = string.Empty;
                    if (FrameSets.TryDequeue(out path))
                    {
                        updating = true;
                        Image image = ImageHelper.GetImage(path, FileHelper.GetFileStream);
                        if (image != null)
                        {
                            _logger.Debug("updating " + path);
                            UpdateFrameToUI(image);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Debug(ex.ToString());
                }
                finally
                {
                    updating = false;
                }
            }
        }
Exemple #14
0
        void _Update()
        {
            _SoulProvider.Update();

            RequestPackage pkg;

            while (_Requests.TryDequeue(out pkg))
            {
                System.Threading.Interlocked.Decrement(ref _TotalRequest);
                Request request = _TryGetRequest(pkg);

                if (request != null)
                {
                    if (_InvokeMethodEvent != null)
                    {
                        _InvokeMethodEvent(request.EntityId, request.MethodId, request.ReturnId, request.MethodParams);
                    }
                }
            }

            ResponsePackage[] responses = _ResponsePop();

            if (responses.Length > 0)
            {
                _Writer.Push(responses);
            }
        }
        void _worker_DoWork(object sender, DoWorkEventArgs e)
        {
            WorkerArgs args = (e.Argument as WorkerArgs);

            if (args == null)
            {
                return;
            }
            lock (_launchTimingLock)
            {
                _lastLaunchInitiatedUtc = DateTime.UtcNow;
            }
            int serverIndex = 0;

            System.Collections.Concurrent.ConcurrentQueue <LaunchItem> globalQueue = args.ConcurrentLaunchQueue;
            int serverTotal = globalQueue.Count;

            if (serverTotal == 0)
            {
                return;
            }

            LaunchItem launchItem         = null;
            var        accountLaunchTimes = _gameSessionMap.GetLaunchAccountTimes();

            while (globalQueue.TryDequeue(out launchItem))
            {
                LaunchManager mgr = new LaunchManager(_launcherLocation, launchItem, accountLaunchTimes);
                mgr.ReportStatusEvent += (status, item) => HandleLaunchMgrStatus(status, item, serverIndex, serverTotal);
                LaunchManager.LaunchManagerResult launchResult;
                GameSession session = null;
                try
                {
                    session = _gameSessionMap.StartLaunchingSession(launchItem.ServerName, launchItem.AccountName);
                    UpdateAccountStatus(ServerAccountStatusEnum.Starting, launchItem);
                    launchResult = mgr.LaunchGameHandlingDelaysAndTitles(_worker);
                }
                finally
                {
                    _gameSessionMap.EndLaunchingSession(launchItem.ServerName, launchItem.AccountName);
                }

                if (launchResult.Success)
                {
                    ++serverIndex;
                    // Let's just wait for game monitor to check if the character list changed
                    // b/c the AccountManager is subscribed for that event
                    //CallUiNotifyAvailableCharactersChanged(); // Pick up any characters - experimental 2017-04-10
                    // CallUiLoadUserAccounts(); // Pick up any characters - before 2017-04-10
                    _gameSessionMap.StartSessionWatcher(session);
                    workerReportProgress("Launched", launchItem, serverIndex, serverTotal);
                }

                if (_worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
Exemple #16
0
        public async System.Threading.Tasks.Task Test()
        {
            var listener = new Regulus.Network.Web.Listener();

            listener.Bind("http://127.0.0.1:12345/");
            var peers = new System.Collections.Concurrent.ConcurrentQueue <Web.Peer>();

            listener.AcceptEvent += peers.Enqueue;

            var connecter     = new Regulus.Network.Web.Connecter(new System.Net.WebSockets.ClientWebSocket());
            var connectResult = await connecter.ConnectAsync("ws://127.0.0.1:12345/");

            Xunit.Assert.True(connectResult);

            var ar = new Regulus.Utility.AutoPowerRegulator(new Utility.PowerRegulator());

            Web.Peer peer;
            while (!peers.TryDequeue(out peer))
            {
                ar.Operate();
            }
            IStreamable server = peer;
            var         serverReceiveBuffer = new byte[5];
            var         serverReceiveTask   = server.Receive(serverReceiveBuffer, 0, 5);
            IStreamable client          = connecter;
            var         clientSendCount = await client.Send(new byte[] { 1, 2, 3, 4, 5 }, 0, 5);

            var serverReceiveCount = await serverReceiveTask;

            Xunit.Assert.Equal(5, serverReceiveCount);
            Xunit.Assert.Equal(5, clientSendCount);
        }
 private void OnStart(object state)
 {
     while (true)
     {
         try
         {
             LockInfo result = null;
             if (mLockInfoQueue.TryDequeue(out result))
             {
                 if (result.Type == ProcessType.Detection)
                 {
                     OnDetection();
                 }
                 else
                 {
                     LockerQueue lockerQueue = GetQueue(result.Key);
                     lockerQueue.Push(result);
                 }
             }
             else
             {
                 System.Threading.Thread.Sleep(10);
             }
         }
         catch (Exception e_)
         {
             Node.Loger.Process(LogType.DEBUG, "DMonitoCenter start error {0}", e_.Message);
         }
     }
 }
Exemple #18
0
        public static CallItem PickCall()
        {
            CallItem outi = null;

            callitem.TryDequeue(out outi);
            return(outi);
        }
Exemple #19
0
 public static void Clear <T>(this System.Collections.Concurrent.ConcurrentQueue <T> concurrentQueue)
 {
     while (concurrentQueue.TryDequeue(out _))
     {
         // Empty
     }
 }
Exemple #20
0
        private T Dequeue()
        {
            T item;

            mQueue.TryDequeue(out item);
            return(item);
        }
        void UpdateLogPrint(object sender, EventArgs e)
        {
            // 너무 이 작업만 할 수 없으므로 일정 작업 이상을 하면 일단 패스한다.
            int logWorkCount = 0;

            while (true)
            {
                ReportData report;

                if (logMsgQueue.TryDequeue(out report) == false)
                {
                    break;
                }

                ++logWorkCount;

                if (listBoxLog.Items.Count > 512)
                {
                    listBoxLog.Items.RemoveAt(0);
                    //listBoxLog.Items.Clear();
                }

                listBoxLog.Items.Add(report);
                listBoxLog.SelectedIndex = listBoxLog.Items.Count - 1;

                if (logWorkCount > 16)
                {
                    break;
                }
            }
        }
Exemple #22
0
        static void DoConcurrentQueue()
        {
            var ShapeQueue = new System.Collections.Concurrent.ConcurrentQueue <int>();

            for (int i = 0; i < 1000; i++)
            {
                ShapeQueue.Enqueue(i);
            }

            int count = 0;

            for (int i = 0; i < 10; i++)
            {
                System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                {
                    //lock ((ShapeQueue as ICollection).SyncRoot)
                    {
                        int outint;
                        while (ShapeQueue.TryDequeue(out outint))
                        {
                            Console.WriteLine(System.Threading.Interlocked.Increment(ref count));
                            Console.WriteLine(outint);
                        }
                    }
                });
            }
        }
Exemple #23
0
        static CatService()
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                while (true)
                {
                    try
                    {
                        if (requests.Count == 0)
                        {
                            requestHandler.WaitOne();
                        }

                        var flight = string.Empty;
                        if (!requests.TryDequeue(out flight))
                        {
                            continue;
                        }

                        var result = new Cat(flight, FrmMain.MonitorProcess).Catch();
                        var match  = Regex.Match(result, "flightNo\":\"(?<value>.*?)\"");
                        if (match.Success)
                        {
                            var f = match.Groups["value"].Value.Trim();
                            results.AddOrUpdate(f, result, (x, y) => y);

                            waitHandler.Set();
                        }
                    }
                    catch { }
                }
            }, null);
        }
Exemple #24
0
        void IRequestQueue.Update()
        {
            if (_Connected() == false)
            {
                Disconnect();
                DisconnectEvent();
                return;
            }

            _SoulProvider.Update();

            RequestPackage pkg;

            while (_Requests.TryDequeue(out pkg))
            {
                System.Threading.Interlocked.Decrement(ref _TotalRequest);
                var request = _TryGetRequest(pkg);

                if (request != null)
                {
                    if (_InvokeMethodEvent != null)
                    {
                        _InvokeMethodEvent(request.EntityId, request.MethodId, request.ReturnId, request.MethodParams);
                    }
                }
            }

            var responses = _ResponsePop();

            if (responses.Length > 0)
            {
                _Writer.Push(responses);
            }
        }
Exemple #25
0
        public object DequeueReceiveMessage()
        {
            object result;

            mReceiveMessages.TryDequeue(out result);
            return(result);
        }
Exemple #26
0
        private object DequeueSendMessage()
        {
            object result;

            mSendMessages.TryDequeue(out result);
            return(result);
        }
Exemple #27
0
 protected override void Run()
 {
     try {
         if (ServiceAgentMode == ServiceAgentMode.Server)
         {
             while (true)
             {
                 Console.Write("path for file to share: ");
                 string p = MainClass.ConsoleReadLine();
                 if (p == "done")
                 {
                     return;
                 }
                 SendMessage(p);
             }
         }
         while (true)
         {
             FileShareMessage fileShareMessage;
             mre_fileReceived.WaitOne(10);
             while (files.TryDequeue(out fileShareMessage))
             {
                 mre_fileReceived.Reset();
                 Console.Write("server has shared a file: ");
                 Console.WriteLine(fileShareMessage.Text);
                 using (TransparentStream s = OpenStream(fileShareMessage.StreamID)) {
                     Console.Write("path to save: ");
                     s.CopyTo(File.OpenWrite(MainClass.ConsoleReadLine()), 64, 512);
                 }
             }
         }
     } catch (System.Threading.ThreadAbortException) {
     }
 }
Exemple #28
0
 /// <summary>
 /// 线程处理器
 /// </summary>
 protected virtual void Run()
 {
     while (!this.IsDelete)
     {
         while (msgs.Count > 0)
         {
             CreateFile();
             if (CommUtil.LOG_PRINT_FILE_BUFFER)
             {
                 /*双缓冲,减少磁盘IO*/
                 for (int i = 0; i < 1000; i++)
                 {
                     String msg;
                     if (msgs.TryDequeue(out msg))
                     {
                         wStream.Write(msg);
                     }
                     else
                     {
                         break;
                     }
                 }
                 /*输入流到文件*/
                 wStream.Flush();
                 fStream.Flush();
             }
             else
             {
                 String msg;
                 if (msgs.TryDequeue(out msg))
                 {
                     /*输入流到文件*/
                     wStream.Write(msg);
                     wStream.Flush();
                     fStream.Flush();
                 }
                 else
                 {
                     break;
                 }
             }
         }
         /*设置无限制等待*/
         are.Reset();
         are.WaitOne();
     }
 }
Exemple #29
0
        private void CheckInitialization()
        {
            if (!_initialized)
            {
                Task.Factory.StartNew(async() => {
                    for (int i = 0; i < 5; i++)
                    {
                        try
                        {
                            var connectionStringResolver = DataAccessManager.GetConnectionStringResolver();
                            if (connectionStringResolver == null)
                            {
                                throw new InvalidOperationException($"Невозможно получить строку подключения. Задайте поставщик строк подключения в {nameof(DataAccessManager)}.{nameof(DataAccessManager.SetConnectionStringResolver)}.");
                            }

                            var connString = connectionStringResolver.ResolveConnectionStringForDataContext(new Type[] { });
                            if (!string.IsNullOrEmpty(connString))
                            {
                                try
                                {
                                    using (var db = new Context())
                                    {
                                        db.DataContext.ExecuteQuery("IF OBJECT_ID (N'Hangfire.Job', N'U')  IS NOT NULL DELETE FROM Hangfire.Job");
                                    }
                                }
                                catch { }

                                GlobalConfiguration.Configuration.UseSqlServerStorage(connString);

                                _server = new BackgroundJobServer();

                                // Удаление всех запланированных задач. Учитывая, что сейчас задачи добавляются через AddOrUpdate, можно обойтись без регулярного удаления всех задач. Если будут возникать ошибочные задачи, их надо будет удалять через какой-то отдельный механизм.
                                // Пока что уберем удаление. Перенес его в DeleteAllTasks.
                                //try
                                //{
                                //    using (var connection = JobStorage.Current.GetConnection())
                                //    {
                                //        connection.GetRecurringJobs().ForEach(x => RecurringJob.RemoveIfExists(x.Id));
                                //    }
                                //}
                                //catch { }

                                Action action = null;
                                while (_actionsQueue.TryDequeue(out action))
                                {
                                    action();
                                }

                                break;
                            }
                        }
                        catch (Exception ex) { Debug.WriteLine("Hangfire.Init error: {0}", ex.ToString()); }

                        await Task.Delay(5000);
                    }
                });
            }
        }
Exemple #30
0
        static public bool GetLog(out string msg)
        {
            if (logMsgQueue.TryDequeue(out msg))
            {
                return(true);
            }

            return(false);
        }
        public RegionData LoadBackup(string file)
        {
            if (!File.Exists(file))
                return null;

            var stream = ArchiveHelpers.GetStream(file);
            if (stream == null)
                return null;

            GZipStream m_loadStream = new GZipStream(stream, CompressionMode.Decompress);
            TarArchiveReader reader = new TarArchiveReader(m_loadStream);
            List<uint> foundLocalIDs = new List<uint>();
            RegionData regiondata = new RegionData();
            regiondata.Init();

            byte[] data;
            string filePath;
            TarArchiveReader.TarEntryType entryType;
            System.Collections.Concurrent.ConcurrentQueue<byte[]> groups =
                new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
            //Load the archive data that we need
            while ((data = reader.ReadEntry(out filePath, out entryType)) != null)
            {
                if (TarArchiveReader.TarEntryType.TYPE_DIRECTORY == entryType)
                    continue;

                if (filePath.StartsWith("parcels/"))
                {
                    //Only use if we are not merging
                    LandData parcel = new LandData();
                    OSD parcelData = OSDParser.DeserializeLLSDBinary(data);
                    parcel.FromOSD((OSDMap) parcelData);
                    if (parcel.OwnerID != UUID.Parse("05948863-b678-433e-87a4-e44d17678d1d"))
                        //The default owner of the 'default' region
                        regiondata.Parcels.Add(parcel);
                }
                else if (filePath.StartsWith("newstyleterrain/"))
                {
                    regiondata.Terrain = data;
                }
                else if (filePath.StartsWith("newstylerevertterrain/"))
                {
                    regiondata.RevertTerrain = data;
                }
                else if (filePath.StartsWith("newstylewater/"))
                {
                    regiondata.Water = data;
                }
                else if (filePath.StartsWith("newstylerevertwater/"))
                {
                    regiondata.RevertWater = data;
                }
                else if (filePath.StartsWith("entities/"))
                {
                    groups.Enqueue(data);
                }
                else if (filePath.StartsWith("regioninfo/"))
                {
                    RegionInfo info = new RegionInfo();
                    info.FromOSD((OSDMap) OSDParser.DeserializeLLSDBinary(data));
                    regiondata.RegionInfo = info;
                }
                data = null;
            }
            m_loadStream.Close();
            m_loadStream = null;

            int threadCount = groups.Count > 16 ? 16 : groups.Count;
            System.Threading.Thread[] threads = new System.Threading.Thread[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                threads[i] = new System.Threading.Thread(() =>
                                                             {
                                                                 byte[] groupData;
                                                                 while (groups.TryDequeue(out groupData))
                                                                 {
                                                                     MemoryStream ms = new MemoryStream(groupData);
                                                                     ISceneEntity sceneObject =
                                                                         SceneEntitySerializer.SceneObjectSerializer
                                                                                              .FromXml2Format(ref ms,
                                                                                                              null);
                                                                     ms.Close();
                                                                     ms = null;
                                                                     data = null;
                                                                     if (sceneObject != null)
                                                                     {
                                                                         foreach (
                                                                             ISceneChildEntity part in
                                                                                 sceneObject.ChildrenEntities())
                                                                         {
                                                                             lock (foundLocalIDs)
                                                                             {
                                                                                 if (
                                                                                     !foundLocalIDs.Contains(
                                                                                         part.LocalId))
                                                                                     foundLocalIDs.Add(part.LocalId);
                                                                                 else
                                                                                     part.LocalId = 0;
                                                                                         //Reset it! Only use it once!
                                                                             }
                                                                         }
                                                                         regiondata.Groups.Add(
                                                                             sceneObject as SceneObjectGroup);
                                                                     }
                                                                 }
                                                             });
                threads[i].Start();
            }
            for (int i = 0; i < threadCount; i++)
                threads[i].Join();

            foundLocalIDs.Clear();

            return regiondata;
        }
Exemple #32
0
        public void TransformStream(Stream a_stream, long a_length = -1)
        {
            Debug.Assert((a_length == -1 || a_length > 0));

            if (a_stream.CanSeek)
            {
                if (a_length > -1)
                {
                    if (a_stream.Position + a_length > a_stream.Length)
                        throw new IndexOutOfRangeException();
                }

                if (a_stream.Position >= a_stream.Length)
                    return;
            }

            System.Collections.Concurrent.ConcurrentQueue<byte[]> queue =
                new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
            System.Threading.AutoResetEvent data_ready = new System.Threading.AutoResetEvent(false);
            System.Threading.AutoResetEvent prepare_data = new System.Threading.AutoResetEvent(false);

            Task reader = Task.Factory.StartNew(() =>
            {
                long total = 0;

                for (; ; )
                {
                    byte[] data = new byte[BUFFER_SIZE];
                    int readed = a_stream.Read(data, 0, data.Length);

                    if ((a_length == -1) && (readed != BUFFER_SIZE))
                        data = data.SubArray(0, readed);
                    else if ((a_length != -1) && (total + readed >= a_length))
                        data = data.SubArray(0, (int)(a_length - total));

                    total += data.Length;

                    queue.Enqueue(data);
                    data_ready.Set();

                    if (a_length == -1)
                    {
                        if (readed != BUFFER_SIZE)
                            break;
                    }
                    else if (a_length == total)
                        break;
                    else if (readed != BUFFER_SIZE)
                        throw new EndOfStreamException();

                    prepare_data.WaitOne();
                }
            });

            Task hasher = Task.Factory.StartNew((obj) =>
            {
                IHash h = (IHash)obj;
                long total = 0;

                for (; ; )
                {
                    data_ready.WaitOne();

                    byte[] data;
                    queue.TryDequeue(out data);

                    prepare_data.Set();

                    total += data.Length;

                    if ((a_length == -1) || (total < a_length))
                    {
                        h.TransformBytes(data, 0, data.Length);
                    }
                    else
                    {
                        int readed = data.Length;
                        readed = readed - (int)(total - a_length);
                        h.TransformBytes(data, 0, data.Length);
                    }

                    if (a_length == -1)
                    {
                        if (data.Length != BUFFER_SIZE)
                            break;
                    }
                    else if (a_length == total)
                        break;
                    else if (data.Length != BUFFER_SIZE)
                        throw new EndOfStreamException();
                }
            }, this);

            reader.Wait();
            hasher.Wait();
        }
        protected virtual void ReadBackup(IScene scene)
        {
            MainConsole.Instance.Debug("[FileBasedSimulationData]: Reading file for " + scene.RegionInfo.RegionName);
            List<uint> foundLocalIDs = new List<uint>();
            var stream = ArchiveHelpers.GetStream((m_loadDirectory == "" || m_loadDirectory == "/")
                                                      ? m_fileName
                                                      : Path.Combine(m_loadDirectory, m_fileName));
            if(stream == null)
                return;

            GZipStream m_loadStream = new GZipStream(stream, CompressionMode.Decompress);
            TarArchiveReader reader = new TarArchiveReader(m_loadStream);

            byte[] data;
            string filePath;
            TarArchiveReader.TarEntryType entryType;
            System.Collections.Concurrent.ConcurrentQueue<byte[]> groups = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
            //Load the archive data that we need
            while ((data = reader.ReadEntry(out filePath, out entryType)) != null)
            {
                if (TarArchiveReader.TarEntryType.TYPE_DIRECTORY == entryType)
                    continue;

                if (filePath.StartsWith("parcels/"))
                {
                    //Only use if we are not merging
                    LandData parcel = new LandData();
                    OSD parcelData = OSDParser.DeserializeLLSDBinary(data);
                    parcel.FromOSD((OSDMap) parcelData);
                    m_parcels.Add(parcel);
                }
                else if (filePath.StartsWith("terrain/"))
                {
                    m_oldstyleterrain = data;
                }
                else if (filePath.StartsWith("revertterrain/"))
                {
                    m_oldstylerevertTerrain = data;
                }
                else if (filePath.StartsWith("newstyleterrain/"))
                {
                    m_terrain = data;
                }
                else if (filePath.StartsWith("newstylerevertterrain/"))
                {
                    m_revertTerrain = data;
                }
                else if (filePath.StartsWith("newstylewater/"))
                {
                    m_water = data;
                }
                else if (filePath.StartsWith("newstylerevertwater/"))
                {
                    m_revertWater = data;
                }
                else if (filePath.StartsWith("entities/"))
                {
                    groups.Enqueue(data);
                }
                data = null;
            }
            m_loadStream.Close();
            m_loadStream = null;

            int threadCount = groups.Count > 16 ? 16 : groups.Count;
            System.Threading.Thread[] threads = new System.Threading.Thread[threadCount];
            for (int i = 0; i < threadCount; i++)
            {
                threads[i] = new System.Threading.Thread(() =>
                    {
                        byte[] groupData;
                        while(groups.TryDequeue(out groupData))
                        {
                            MemoryStream ms = new MemoryStream(groupData);
                            SceneObjectGroup sceneObject = SceneObjectSerializer.FromXml2Format(ref ms, scene);
                            ms.Close();
                            ms = null;
                            data = null;
                            if (sceneObject != null)
                            {
                                foreach (ISceneChildEntity part in sceneObject.ChildrenEntities())
                                {
                                    lock (foundLocalIDs)
                                    {
                                        if (!foundLocalIDs.Contains(part.LocalId))
                                            foundLocalIDs.Add(part.LocalId);
                                        else
                                            part.LocalId = 0; //Reset it! Only use it once!
                                    }
                                }
                                m_groups.Add(sceneObject);
                            }
                        }
                    });
                threads[i].Start();
            }
            for (int i = 0; i < threadCount; i++)
                threads[i].Join();


            foundLocalIDs.Clear();
            GC.Collect();
        }