Skip to content

A Swiss Army knife for developers.

Fixed bug #94 where DevToys takes several seconds to shutdown after using PNG / JPEG Compressor tool. (#95)

Etienne Baudouxd10390bc87ac
diff --git a/src/dev/impl/DevToys.OutOfProcService/API/Core/OOP/IOutOfProcService.cs b/src/dev/impl/DevToys.OutOfProcService/API/Core/OOP/IOutOfProcService.cs
index ac56d846a..73f07ff19 100644
--- a/src/dev/impl/DevToys.OutOfProcService/API/Core/OOP/IOutOfProcService.cs
+++ b/src/dev/impl/DevToys.OutOfProcService/API/Core/OOP/IOutOfProcService.cs
@@ -13,6 +13,6 @@ namespace DevToys.OutOfProcService.API.Core.OOP
event EventHandler<AppServiceProgressMessageEventArgs>? ReportProgress;
- Task<AppServiceMessageBase> ProcessMessageAsync(AppServiceMessageBase inputMessage, CancellationToken cancellationToken);
+ Task<AppServiceMessageBase?> ProcessMessageAsync(AppServiceMessageBase inputMessage, CancellationToken cancellationToken);
}
}
diff --git a/src/dev/impl/DevToys.OutOfProcService/Core/OOP/AppService.cs b/src/dev/impl/DevToys.OutOfProcService/Core/OOP/AppService.cs
index defbad465..38ce1ebe3 100644
--- a/src/dev/impl/DevToys.OutOfProcService/Core/OOP/AppService.cs
+++ b/src/dev/impl/DevToys.OutOfProcService/Core/OOP/AppService.cs
@@ -208,11 +208,14 @@ namespace DevToys.OutOfProcService.Core.OOP
service.ReportProgress += Service_ReportProgress;
// Invoke the service.
- AppServiceMessageBase outputMessage = await service.ProcessMessageAsync(inputMessage, cancellationToken);
- outputMessage.MessageId = inputMessage.MessageId;
+ AppServiceMessageBase? outputMessage = await service.ProcessMessageAsync(inputMessage, cancellationToken);
+ if (outputMessage is not null)
+ {
+ outputMessage.MessageId = inputMessage.MessageId;
- // Send the service result as a response to the UWP app.
- await SendMessageAsync(outputMessage);
+ // Send the service result as a response to the UWP app.
+ await SendMessageAsync(outputMessage);
+ }
}
catch (OperationCanceledException)
{
diff --git a/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/OutOfProcServiceBase.cs b/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/OutOfProcServiceBase.cs
index 27efff4e1..fc2964385 100644
--- a/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/OutOfProcServiceBase.cs
+++ b/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/OutOfProcServiceBase.cs
@@ -10,7 +10,7 @@ namespace DevToys.OutOfProcService.OutOfProcServices
{
internal abstract class OutOfProcServiceBase<TInput, TOutput> : IOutOfProcService
where TInput : AppServiceMessageBase
- where TOutput : AppServiceMessageBase
+ where TOutput : AppServiceMessageBase?
{
private readonly DisposableSempahore _sempahore = new();
private bool _messageIsProcessing;
@@ -19,7 +19,7 @@ namespace DevToys.OutOfProcService.OutOfProcServices
public event EventHandler<AppServiceProgressMessageEventArgs>? ReportProgress;
- public async Task<AppServiceMessageBase> ProcessMessageAsync(AppServiceMessageBase inputMessage, CancellationToken cancellationToken)
+ public async Task<AppServiceMessageBase?> ProcessMessageAsync(AppServiceMessageBase inputMessage, CancellationToken cancellationToken)
{
Assumes.NotNull(inputMessage.MessageId, nameof(inputMessage.MessageId));
diff --git a/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/ShutdownService.cs b/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/ShutdownService.cs
index 5079534c9..a3160516c 100644
--- a/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/ShutdownService.cs
+++ b/src/dev/impl/DevToys.OutOfProcService/OutOfProcServices/ShutdownService.cs
@@ -3,12 +3,13 @@ using System.Threading;
using System.Threading.Tasks;
using DevToys.OutOfProcService.API.Core.OOP;
using DevToys.OutOfProcService.Core.OOP;
+using DevToys.Shared.Core.OOP;
namespace DevToys.OutOfProcService.OutOfProcServices
{
[Export(typeof(IOutOfProcService))]
[InputType(typeof(ShutdownMessage))]
- internal sealed class ShutdownService : OutOfProcServiceBase<ShutdownMessage, ShutdownMessage>
+ internal sealed class ShutdownService : OutOfProcServiceBase<ShutdownMessage, AppServiceMessageBase?>
{
private readonly AppService _appService;
@@ -18,10 +19,10 @@ namespace DevToys.OutOfProcService.OutOfProcServices
_appService = appService;
}
- protected override Task<ShutdownMessage> ProcessMessageAsync(ShutdownMessage inputMessage, CancellationToken cancellationToken)
+ protected override Task<AppServiceMessageBase?> ProcessMessageAsync(ShutdownMessage inputMessage, CancellationToken cancellationToken)
{
_appService.IndicateAppServiceConnectionLost();
- return Task.FromResult(inputMessage);
+ return Task.FromResult<AppServiceMessageBase?>(null);
}
}
}
diff --git a/src/dev/impl/DevToys/Api/Core/OOP/IAppService.cs b/src/dev/impl/DevToys/Api/Core/OOP/IAppService.cs
index 674f08b37..581b34279 100644
--- a/src/dev/impl/DevToys/Api/Core/OOP/IAppService.cs
+++ b/src/dev/impl/DevToys/Api/Core/OOP/IAppService.cs
@@ -10,6 +10,8 @@ namespace DevToys.Api.Core.OOP
/// </summary>
public interface IAppService
{
+ Task SendMessageAsync(AppServiceMessageBase message);
+
Task<T> SendMessageAndGetResponseAsync<T>(AppServiceMessageBase message) where T : AppServiceMessageBase;
Task<T> SendMessageAndGetResponseAsync<T>(AppServiceMessageBase message, CancellationToken cancellationToken) where T : AppServiceMessageBase;
diff --git a/src/dev/impl/DevToys/Core/OOP/AppService.cs b/src/dev/impl/DevToys/Core/OOP/AppService.cs
index 87b792f03..152a39293 100644
--- a/src/dev/impl/DevToys/Core/OOP/AppService.cs
+++ b/src/dev/impl/DevToys/Core/OOP/AppService.cs
@@ -4,6 +4,7 @@ using System;
using System.Collections.Concurrent;
using System.Composition;
using System.IO.Pipes;
+using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -36,6 +37,15 @@ namespace DevToys.Core.OOP
App.Current.Suspending += OnAppSuspending;
}
+ public async Task SendMessageAsync(AppServiceMessageBase message)
+ {
+ await InternalSendMessageAndGetResponseAsync(
+ waitForResult: false,
+ message,
+ DummyProgress.DefaultInstance,
+ CancellationToken.None);
+ }
+
public Task<T> SendMessageAndGetResponseAsync<T>(AppServiceMessageBase message) where T : AppServiceMessageBase
{
return SendMessageAndGetResponseAsync<T>(message, CancellationToken.None);
@@ -55,7 +65,13 @@ namespace DevToys.Core.OOP
{
Arguments.NotNull(progress, nameof(progress));
- AppServiceMessageBase? result = await InternalSendMessageAndGetResponseAsync(message, progress, cancellationToken);
+ AppServiceMessageBase? result
+ = await InternalSendMessageAndGetResponseAsync(
+ waitForResult: true,
+ message,
+ progress,
+ cancellationToken);
+
if (result is null)
{
throw new Exception("Unable to send a message to the app service.");
@@ -73,14 +89,14 @@ namespace DevToys.Core.OOP
SuspendingDeferral? deferral = e.SuspendingOperation.GetDeferral();
// Shut down the Win32 app.
- await SendMessageAndGetResponseAsync<ShutdownMessage>(new ShutdownMessage());
+ await SendMessageAsync(new ShutdownMessage());
Disconnect();
deferral.Complete();
}
- private async Task<AppServiceMessageBase?> InternalSendMessageAndGetResponseAsync(AppServiceMessageBase message, IProgress<AppServiceProgressMessage> progress, CancellationToken cancellationToken)
+ private async Task<AppServiceMessageBase?> InternalSendMessageAndGetResponseAsync(bool waitForResult, AppServiceMessageBase message, IProgress<AppServiceProgressMessage> progress, CancellationToken cancellationToken)
{
Arguments.NotNull(message, nameof(message));
Arguments.NotNull(progress, nameof(progress));
@@ -103,23 +119,29 @@ namespace DevToys.Core.OOP
message.MessageId = messageId;
// start tracking the message.
- _inProgressMessages.TryAdd(message.MessageId.Value, messageCompletionSource);
- _progressReporters.TryAdd(message.MessageId.Value, progress);
+ if (waitForResult)
+ {
+ _inProgressMessages.TryAdd(message.MessageId.Value, messageCompletionSource);
+ _progressReporters.TryAdd(message.MessageId.Value, progress);
+ }
// Send the message.
SendMessage(message);
}
- // Wait for the answer of the app service.
- using (CancellationTokenRegistration cancellationTokenRegistration
- = cancellationToken.Register(() =>
- {
- OnSendMessageCanceledAsync(messageId).Forget();
- }))
+ if (waitForResult)
{
- AppServiceMessageBase result = await messageCompletionSource.Task;
- _progressReporters.TryRemove(message.MessageId.Value, out _);
- return result;
+ // Wait for the answer of the app service.
+ using (CancellationTokenRegistration cancellationTokenRegistration
+ = cancellationToken.Register(() =>
+ {
+ OnSendMessageCanceledAsync(messageId).Forget();
+ }))
+ {
+ AppServiceMessageBase result = await messageCompletionSource.Task;
+ _progressReporters.TryRemove(message.MessageId.Value, out _);
+ return result;
+ }
}
}
catch (Exception ex)