Skip to content

Commit

Permalink
[Win32WindowService] Updated service to the refactored setup
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingpie committed Oct 30, 2024
1 parent 2c5cd82 commit c1d2271
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 47 deletions.
3 changes: 2 additions & 1 deletion src/20-Services/Wtq.Services.Win32/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
global using System.Threading.Tasks;
global using Wtq.Configuration;
global using Wtq.Exceptions;
global using Wtq.Utils;
global using Wtq.Utils;
global using Wtq.Utils.AsyncInit;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Wtq.Services;

namespace Wtq.Services.Win32;

Expand All @@ -8,6 +7,6 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddWin32ProcessService(this IServiceCollection services)
{
return services
.AddSingletonHostedService<IWtqProcessService, Win32ProcessService>();
.AddSingleton<IWtqWindowService, Win32WindowService>();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using Microsoft.Extensions.Hosting;
using Wtq.Configuration;
using Wtq.Exceptions;
using Wtq.Services.Win32.Extensions;
using Wtq.Services.Win32.Native;
using Wtq.Utils;

namespace Wtq.Services.Win32;

public sealed class Win32ProcessService :
public sealed class Win32WindowService :
IAsyncInitializable,
IDisposable,
IHostedService,
IWtqProcessService
IWtqWindowService
{
private readonly ILogger _log = Log.For<Win32ProcessService>();
private readonly ILogger _log = Log.For<Win32WindowService>();
private readonly TimeSpan _lookupInterval = TimeSpan.FromSeconds(2);
private readonly SemaphoreSlim _lock = new(1);

Expand All @@ -26,6 +22,11 @@ public async Task CreateAsync(WtqAppOptions opts)
await CreateProcessAsync(opts).ConfigureAwait(false);
}

public async Task InitializeAsync()
{
await UpdateProcessesAsync().ConfigureAwait(false);
}

public void Dispose()
{
_lock.Dispose();
Expand All @@ -40,22 +41,22 @@ public void Dispose()
return processes.FirstOrDefault(p => p.Matches(opts));
}

public WtqWindow? GetForegroundWindow()
public Task<WtqWindow?> GetForegroundWindowAsync()
{
try
{
var fg = GetForegroundProcessId();
if (fg > 0)
{
return new Win32WtqProcess(Process.GetProcessById((int)fg));
return Task.FromResult<WtqWindow?>(new Win32WtqWindow(Process.GetProcessById((int)fg)));
}
}
catch (Exception ex)
{
_log.LogWarning(ex, "Error looking up foreground process: {Message}", ex.Message);
}

return null;
return Task.FromResult<WtqWindow?>(null);
}

public async Task<ICollection<WtqWindow>> GetWindowsAsync()
Expand All @@ -65,16 +66,6 @@ public async Task<ICollection<WtqWindow>> GetWindowsAsync()
return _processes;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
await UpdateProcessesAsync().ConfigureAwait(false);
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

private static uint GetForegroundProcessId()
{
var hwnd = User32.GetForegroundWindow();
Expand All @@ -94,31 +85,21 @@ private async Task CreateProcessAsync(WtqAppOptions opts)
FileName = opts.FileName,
Arguments = opts.Arguments,
UseShellExecute = false,
Environment =
{
{ "WTQ_START", opts.Name },
},
};

// Start
Retry.Default
.Execute(
() =>
{
try
{
process.Start();
}
catch (Win32Exception ex) when (ex.Message == "The system cannot find the file specified")
{
throw new CancelRetryException($"Could not start process using file name '{opts.FileName}'. Make sure it exists and the configuration is correct");
}
catch (Exception ex)
{
_log.LogError(ex, "Error starting process: {Message}", ex.Message);
throw new WtqException($"Error starting instance of app '{opts}': {ex.Message}", ex);
}
});
try
{
process.Start();
}
catch (Win32Exception ex) when (ex.Message == "The system cannot find the file specified")
{
throw new WtqException($"Could not start process using file name '{opts.FileName}'. Make sure it exists and the configuration is correct");
}
catch (Exception ex)
{
throw new WtqException($"Error starting process for app '{opts}': {ex.Message}", ex);
}

await UpdateProcessesAsync(force: true).NoCtx();
}
Expand Down Expand Up @@ -155,7 +136,7 @@ private async Task UpdateProcessesAsync(bool force = false)
continue;
}

var wtqProcess = new Win32WtqProcess(proc);
var wtqProcess = new Win32WtqWindow(proc);
res.Add(wtqProcess);
}

Expand Down

0 comments on commit c1d2271

Please sign in to comment.