mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
Use task delay instead of thread sleep (#7401)
* Use task delay instead of thread.sleep to free up the blocked thread * fix formatting
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Storage
|
||||
{
|
||||
@@ -12,7 +12,7 @@ namespace Microsoft.Plugin.Program.Storage
|
||||
{
|
||||
// To obtain the path of the app when multiple events are added to the Concurrent queue across multiple threads.
|
||||
// On the first occurence of a different file path, the existing app path is to be returned without removing any more elements from the queue.
|
||||
public static string GetAppPathFromQueue(ConcurrentQueue<string> eventHandlingQueue, int dequeueDelay)
|
||||
public static async Task<string> GetAppPathFromQueueAsync(ConcurrentQueue<string> eventHandlingQueue, int dequeueDelay)
|
||||
{
|
||||
if (eventHandlingQueue == null)
|
||||
{
|
||||
@@ -36,7 +36,7 @@ namespace Microsoft.Plugin.Program.Storage
|
||||
}
|
||||
|
||||
// This delay has been added to account for the delay in events being triggered during app installation.
|
||||
Thread.Sleep(dequeueDelay);
|
||||
await Task.Delay(dequeueDelay).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return previousAppPath;
|
||||
|
||||
@@ -8,7 +8,6 @@ using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage;
|
||||
@@ -40,15 +39,15 @@ namespace Microsoft.Plugin.Program.Storage
|
||||
InitializeFileSystemWatchers();
|
||||
|
||||
// This task would always run in the background trying to dequeue file paths from the queue at regular intervals.
|
||||
Task.Run(() =>
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int dequeueDelay = 500;
|
||||
string appPath = EventHandler.GetAppPathFromQueue(commonEventHandlingQueue, dequeueDelay);
|
||||
string appPath = await EventHandler.GetAppPathFromQueueAsync(commonEventHandlingQueue, dequeueDelay).ConfigureAwait(false);
|
||||
|
||||
// To allow for the installation process to finish.
|
||||
Thread.Sleep(5000);
|
||||
await Task.Delay(5000).ConfigureAwait(false);
|
||||
|
||||
if (!string.IsNullOrEmpty(appPath))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user