diff --git a/LiteDB/Engine/Disk/DiskWriterQueue.cs b/LiteDB/Engine/Disk/DiskWriterQueue.cs index d86474a2b..40a3e024f 100644 --- a/LiteDB/Engine/Disk/DiskWriterQueue.cs +++ b/LiteDB/Engine/Disk/DiskWriterQueue.cs @@ -50,12 +50,12 @@ public void EnqueuePage(PageBuffer page) // throw last exception that stop running queue if (_exception != null) throw _exception; + _queueIsEmpty.Reset(); + _queue.Enqueue(page); + _queueHasItems.Set(); + lock (_queueSync) { - _queueIsEmpty.Reset(); - _queue.Enqueue(page); - _queueHasItems.Set(); - if (_task == null) { _task = Task.Factory.StartNew(ExecuteQueue, TaskCreationOptions.LongRunning); @@ -76,7 +76,7 @@ public void Wait() /// /// Execute all items in queue sync /// - private async Task ExecuteQueue() + private void ExecuteQueue() { try { @@ -88,19 +88,17 @@ private async Task ExecuteQueue() } else { - lock (_queueSync) - { - if (_queue.Count > 0) continue; - _queueIsEmpty.Set(); - _queueHasItems.Reset(); + if (_queue.Count > 0) continue; + + _queueIsEmpty.Set(); + _queueHasItems.Reset(); - if (_shouldClose) return; - } + if (_shouldClose) return; _stream.FlushToDisk(); - await _queueHasItems.WaitAsync(); + _queueHasItems.WaitAsync().GetAwaiter().GetResult(); } } } @@ -137,7 +135,7 @@ public void Dispose() _shouldClose = true; _queueHasItems.Set(); // unblock the running loop in case there are no items - _task?.Wait(); + _task?.GetAwaiter().GetResult(); _task = null; } }