Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions MinimalWorker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0932226D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalWorker.Desktop.AvaloniaUI.Sample", "samples\MinimalWorker.Desktop.AvaloniaUI.Sample\MinimalWorker.Desktop.AvaloniaUI.Sample.csproj", "{69762A6B-98A1-46EA-9059-DB13F0610F0E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalWorker.Desktop.Wpf.Sample", "samples\MinimalWorker.Desktop.Wpf.Sample\MinimalWorker.Desktop.Wpf.Sample.csproj", "{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -142,6 +144,18 @@ Global
{69762A6B-98A1-46EA-9059-DB13F0610F0E}.Release|x64.Build.0 = Release|Any CPU
{69762A6B-98A1-46EA-9059-DB13F0610F0E}.Release|x86.ActiveCfg = Release|Any CPU
{69762A6B-98A1-46EA-9059-DB13F0610F0E}.Release|x86.Build.0 = Release|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Debug|x64.ActiveCfg = Debug|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Debug|x64.Build.0 = Debug|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Debug|x86.ActiveCfg = Debug|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Debug|x86.Build.0 = Debug|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Release|Any CPU.Build.0 = Release|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Release|x64.ActiveCfg = Release|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Release|x64.Build.0 = Release|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Release|x86.ActiveCfg = Release|Any CPU
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -156,5 +170,6 @@ Global
{A75FAADC-FA65-499E-A3DA-65A75AB2786E} = {5D20AA90-6969-D8BD-9DCD-8634F4692FDA}
{19723BBE-49D4-49A3-ABDD-122A70F67EAF} = {5D20AA90-6969-D8BD-9DCD-8634F4692FDA}
{69762A6B-98A1-46EA-9059-DB13F0610F0E} = {5D20AA90-6969-D8BD-9DCD-8634F4692FDA}
{B4E4367B-CC76-4A96-B260-4BCF47EAD3E0} = {5D20AA90-6969-D8BD-9DCD-8634F4692FDA}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions samples/MinimalWorker.Desktop.Wpf.Sample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Application x:Class="MinimalWorker.Desktop.Wpf.Sample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnMainWindowClose">
<Application.Resources>
<!-- Catppuccin Mocha palette -->
<SolidColorBrush x:Key="BaseBg" Color="#1e1e2e"/>
<SolidColorBrush x:Key="SurfaceBg" Color="#313244"/>
<SolidColorBrush x:Key="SurfaceBorder" Color="#45475a"/>
<SolidColorBrush x:Key="TextPrimary" Color="#cdd6f4"/>
<SolidColorBrush x:Key="TextMuted" Color="#6c7086"/>
<SolidColorBrush x:Key="AccentBlue" Color="#89b4fa"/>
<SolidColorBrush x:Key="AccentPurple" Color="#cba6f7"/>
<SolidColorBrush x:Key="AccentGreen" Color="#a6e3a1"/>
<SolidColorBrush x:Key="AccentYellow" Color="#f9e2af"/>
</Application.Resources>
</Application>
61 changes: 61 additions & 0 deletions samples/MinimalWorker.Desktop.Wpf.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MinimalWorker;
using MinimalWorker.Desktop.Wpf.Sample.ViewModels;

namespace MinimalWorker.Desktop.Wpf.Sample;

public partial class App : Application
{
private IHost? _host;

protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

var builder = Host.CreateApplicationBuilder(e.Args);
builder.Services.AddSingleton<WorkerStateService>();

_host = builder.Build();

Comment on lines +13 to +21

Copilot AI Feb 19, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OnStartup is async void (WPF requirement), so any exception thrown after an await will crash the process and is difficult to observe. Wrap the async body in try/catch (log + Shutdown(-1)), so startup failures (e.g., host build/start) fail predictably.

Copilot uses AI. Check for mistakes.
// 1) Periodic worker — every 1 second
_host.RunPeriodicBackgroundWorker(TimeSpan.FromSeconds(1), (WorkerStateService state) =>
{
state.RecordExecution("Periodic", $"Tick at {DateTime.Now:HH:mm:ss}");
});

// 2) Slow worker — every 5 seconds (simulates heavier work)
_host.RunPeriodicBackgroundWorker(TimeSpan.FromSeconds(5), async (WorkerStateService state) =>
{
await Task.Delay(200); // simulate async work
state.RecordExecution("SlowTask", $"Completed at {DateTime.Now:HH:mm:ss}");
});

// 3) Cron worker — every minute ("* * * * *")
_host.RunCronBackgroundWorker("* * * * *", (WorkerStateService state) =>
{
state.RecordExecution("Cron", $"Fired at {DateTime.Now:HH:mm:ss}");
});

await _host.StartAsync();

var workerState = _host.Services.GetRequiredService<WorkerStateService>();
var mainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(workerState)
};
MainWindow = mainWindow;
mainWindow.Show();
}

protected override async void OnExit(ExitEventArgs e)
{
if (_host is not null)
{
await _host.StopAsync();
_host.Dispose();
}
Comment on lines +52 to +58

Copilot AI Feb 19, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In OnExit, consider awaiting StopAsync with ConfigureAwait(false) (and optionally a timeout) so shutdown doesn’t depend on resuming on the Dispatcher synchronization context while the app is already exiting.

Copilot uses AI. Check for mistakes.
base.OnExit(e);
}
}
158 changes: 158 additions & 0 deletions samples/MinimalWorker.Desktop.Wpf.Sample/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<Window x:Class="MinimalWorker.Desktop.Wpf.Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MinimalWorker Dashboard"
Width="860" Height="580"
MinWidth="700" MinHeight="480"
Background="{StaticResource BaseBg}"
FontFamily="Segoe UI"
WindowStartupLocation="CenterScreen">

<DockPanel Margin="32">

<!-- Header row with title + uptime -->
<Grid DockPanel.Dock="Top" Margin="0,0,0,24">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<StackPanel Grid.Column="0">
<TextBlock Text="MinimalWorker Dashboard"
FontSize="26" FontWeight="Bold"
Foreground="{StaticResource TextPrimary}"/>
<TextBlock Text="3 background workers running live"
FontSize="13" Foreground="{StaticResource TextMuted}"
Margin="0,4,0,0"/>
</StackPanel>

<Border Grid.Column="1" Background="{StaticResource SurfaceBg}"
CornerRadius="10" Padding="16,10" VerticalAlignment="Center">
<StackPanel>
<TextBlock Text="UPTIME" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"
HorizontalAlignment="Center"/>
<TextBlock Text="{Binding Uptime}"
FontSize="22" FontWeight="Bold"
Foreground="{StaticResource AccentYellow}"
HorizontalAlignment="Center"
Margin="0,2,0,0"/>
</StackPanel>
</Border>
</Grid>

<!-- Activity bar at bottom -->
<Border DockPanel.Dock="Bottom" Background="{StaticResource SurfaceBg}"
CornerRadius="10" Padding="18,14" Margin="0,24,0,0">
<StackPanel Orientation="Horizontal">
<Ellipse Fill="{StaticResource AccentGreen}"
Width="10" Height="10" VerticalAlignment="Center"
Margin="0,0,10,0"/>
<TextBlock Text="{Binding LatestActivity}"
Foreground="{StaticResource TextPrimary}"
FontSize="14" VerticalAlignment="Center"/>
</StackPanel>
</Border>

<!-- 3 Worker cards -->
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<!-- Periodic Worker -->
<Border Grid.Column="0" Background="{StaticResource SurfaceBg}"
CornerRadius="14" Padding="24,20"
BorderBrush="{StaticResource SurfaceBorder}" BorderThickness="1">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,12">
<Ellipse Fill="{StaticResource AccentBlue}"
Width="8" Height="8" VerticalAlignment="Center"
Margin="0,0,8,0"/>
<TextBlock Text="{Binding Periodic.Label}"
FontSize="13" FontWeight="SemiBold"
Foreground="{StaticResource AccentBlue}"/>
</StackPanel>
<TextBlock Text="EXECUTIONS" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"/>
<TextBlock Text="{Binding Periodic.ExecutionCount}"
FontSize="38" FontWeight="Bold"
Foreground="{StaticResource TextPrimary}"
Margin="0,2,0,12"/>
<TextBlock Text="LAST RUN" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"/>
<TextBlock Text="{Binding Periodic.LastRunTime}"
FontSize="16" Foreground="{StaticResource AccentBlue}"
Margin="0,2,0,12"/>
<TextBlock Text="{Binding Periodic.StatusMessage}"
FontSize="12" Foreground="{StaticResource TextMuted}"
TextWrapping="Wrap"/>
</StackPanel>
</Border>

<!-- Slow Task Worker -->
<Border Grid.Column="2" Background="{StaticResource SurfaceBg}"
CornerRadius="14" Padding="24,20"
BorderBrush="{StaticResource SurfaceBorder}" BorderThickness="1">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,12">
<Ellipse Fill="{StaticResource AccentPurple}"
Width="8" Height="8" VerticalAlignment="Center"
Margin="0,0,8,0"/>
<TextBlock Text="{Binding SlowTask.Label}"
FontSize="13" FontWeight="SemiBold"
Foreground="{StaticResource AccentPurple}"/>
</StackPanel>
<TextBlock Text="EXECUTIONS" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"/>
<TextBlock Text="{Binding SlowTask.ExecutionCount}"
FontSize="38" FontWeight="Bold"
Foreground="{StaticResource TextPrimary}"
Margin="0,2,0,12"/>
<TextBlock Text="LAST RUN" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"/>
<TextBlock Text="{Binding SlowTask.LastRunTime}"
FontSize="16" Foreground="{StaticResource AccentPurple}"
Margin="0,2,0,12"/>
<TextBlock Text="{Binding SlowTask.StatusMessage}"
FontSize="12" Foreground="{StaticResource TextMuted}"
TextWrapping="Wrap"/>
</StackPanel>
</Border>

<!-- Cron Worker -->
<Border Grid.Column="4" Background="{StaticResource SurfaceBg}"
CornerRadius="14" Padding="24,20"
BorderBrush="{StaticResource SurfaceBorder}" BorderThickness="1">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,0,12">
<Ellipse Fill="{StaticResource AccentGreen}"
Width="8" Height="8" VerticalAlignment="Center"
Margin="0,0,8,0"/>
<TextBlock Text="{Binding Cron.Label}"
FontSize="13" FontWeight="SemiBold"
Foreground="{StaticResource AccentGreen}"/>
</StackPanel>
<TextBlock Text="EXECUTIONS" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"/>
<TextBlock Text="{Binding Cron.ExecutionCount}"
FontSize="38" FontWeight="Bold"
Foreground="{StaticResource TextPrimary}"
Margin="0,2,0,12"/>
<TextBlock Text="LAST RUN" FontSize="10" FontWeight="SemiBold"
Foreground="{StaticResource TextMuted}"/>
<TextBlock Text="{Binding Cron.LastRunTime}"
FontSize="16" Foreground="{StaticResource AccentGreen}"
Margin="0,2,0,12"/>
<TextBlock Text="{Binding Cron.StatusMessage}"
FontSize="12" Foreground="{StaticResource TextMuted}"
TextWrapping="Wrap"/>
</StackPanel>
</Border>
</Grid>
</DockPanel>
</Window>
11 changes: 11 additions & 0 deletions samples/MinimalWorker.Desktop.Wpf.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Windows;

namespace MinimalWorker.Desktop.Wpf.Sample;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MinimalWorker.Shared.Sample\MinimalWorker.Shared.Sample.csproj" />
<ProjectReference Include="..\..\src\MinimalWorker.Generators\MinimalWorker.Generators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Threading;

namespace MinimalWorker.Desktop.Wpf.Sample.ViewModels;

public class MainWindowViewModel : INotifyPropertyChanged
{
private readonly WorkerStateService _state;
private readonly Dispatcher _dispatcher;
private string _uptime = "00:00:00";
private string _latestActivity = "Starting up...";

public WorkerCardViewModel Periodic { get; } = new("Periodic (1s)");
public WorkerCardViewModel SlowTask { get; } = new("Slow Task (5s)");
public WorkerCardViewModel Cron { get; } = new("Cron (every min)");

public string Uptime
{
get => _uptime;
set => SetField(ref _uptime, value);
}

public string LatestActivity
{
get => _latestActivity;
set => SetField(ref _latestActivity, value);
}

public MainWindowViewModel(WorkerStateService state)
{
_state = state;
_dispatcher = Dispatcher.CurrentDispatcher;

_state.StateChanged += workerName =>
{
_dispatcher.BeginInvoke(() =>
{
var info = _state.GetOrAdd(workerName);
var card = workerName switch
{
"Periodic" => Periodic,
"SlowTask" => SlowTask,
"Cron" => Cron,
_ => null
};
card?.UpdateFrom(info);
LatestActivity = $"[{workerName}] {info.StatusMessage}";
});
};

var uptimeTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
uptimeTimer.Tick += (_, _) => Uptime = _state.Uptime.ToString(@"hh\:mm\:ss");
uptimeTimer.Start();
}

public event PropertyChangedEventHandler? PropertyChanged;

private void SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return;
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Loading
Loading