-
Notifications
You must be signed in to change notification settings - Fork 6
WPF test #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
WPF test #30
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
|
||
| // 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
|
||
| base.OnExit(e); | ||
| } | ||
| } | ||
158 changes: 158 additions & 0 deletions
158
samples/MinimalWorker.Desktop.Wpf.Sample/MainWindow.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
samples/MinimalWorker.Desktop.Wpf.Sample/MainWindow.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
samples/MinimalWorker.Desktop.Wpf.Sample/MinimalWorker.Desktop.Wpf.Sample.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
65 changes: 65 additions & 0 deletions
65
samples/MinimalWorker.Desktop.Wpf.Sample/ViewModels/MainWindowViewModel.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OnStartupisasync void(WPF requirement), so any exception thrown after anawaitwill 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.