Skip to content

Add real-time status to output window #12

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
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
2 changes: 2 additions & 0 deletions SqlcmdGuiApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ private async void ExecuteButton_Click(object sender, RoutedEventArgs e)
process.BeginOutputReadLine();
process.BeginErrorReadLine();

window.AttachProcess(process);

window.Show();

await process.WaitForExitAsync();
Expand Down
15 changes: 13 additions & 2 deletions SqlcmdGuiApp/OutputWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SQLCMD Output" Height="400" Width="600">
<Grid Margin="5">
<DockPanel Margin="5">
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem>
<TextBlock x:Name="StatusTextBlock" Text="Status: Running" />
</StatusBarItem>
<StatusBarItem>
<TextBlock x:Name="DurationTextBlock" Text="Duration: 00:00:00" />
</StatusBarItem>
<StatusBarItem HorizontalAlignment="Right">
<Button x:Name="StopButton" Content="Stop" Width="60" Click="StopButton_Click"/>
</StatusBarItem>
</StatusBar>
<TextBox x:Name="OutputTextBox" IsReadOnly="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
</Grid>
</DockPanel>
</Window>
54 changes: 54 additions & 0 deletions SqlcmdGuiApp/OutputWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;

namespace SqlcmdGuiApp
{
public partial class OutputWindow : Window
{
private DispatcherTimer? _timer;
private readonly Stopwatch _stopwatch = new();
private Process? _process;
private bool _cancelled;

public OutputWindow()
{
InitializeComponent();
Expand All @@ -14,6 +22,36 @@ public OutputWindow(string output, string error) : this()
OutputTextBox.Text = string.IsNullOrWhiteSpace(error) ? output : output + "\n" + error;
}

public void AttachProcess(Process process)
{
_process = process;
_process.Exited += Process_Exited;

_stopwatch.Start();
_timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
_timer.Tick += (s, e) =>
{
DurationTextBlock.Text = $"Duration: {_stopwatch.Elapsed:hh\\:mm\\:ss}";
};
_timer.Start();

StatusTextBlock.Text = "Status: Running";
DurationTextBlock.Text = "Duration: 00:00:00";
}

private void Process_Exited(object? sender, EventArgs e)
{
_timer?.Stop();
_stopwatch.Stop();
Dispatcher.Invoke(() =>
{
string status = _cancelled ? "Cancelled" : (_process?.ExitCode == 0 ? "Completed" : "Failed");
StatusTextBlock.Text = $"Status: {status}";
DurationTextBlock.Text = $"Duration: {_stopwatch.Elapsed:hh\\:mm\\:ss}";
StopButton.IsEnabled = false;
});
}

public void AppendOutput(string text)
{
Dispatcher.Invoke(() =>
Expand All @@ -22,5 +60,21 @@ public void AppendOutput(string text)
OutputTextBox.ScrollToEnd();
});
}

private void StopButton_Click(object sender, RoutedEventArgs e)
{
if (_process != null && !_process.HasExited)
{
_cancelled = true;
try
{
_process.Kill();
}
catch
{
// ignore
}
}
}
}
}