Skip to content

Commit 3f5f289

Browse files
committed
Add elapsed time display and refactor for async
- Added a StatusBar in MainWindow.xaml to show elapsed time for operations. - Introduced a Stopwatch in MainWindow.xaml.cs to measure operation time. - Updated imports, adding System.Diagnostics and removing System.Threading. - Simplified comments for BtnTestXpath_Click and PublishDataGrid. - Modified BtnTestXpath_Click to clear and display elapsed time post-operation. - Refactored PublishDataGrid to async, improving UI responsiveness.
1 parent c2b19a7 commit 3f5f289

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/UiaXpathTester/MainWindow.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,11 @@
6666
Name="LblStatus"
6767
Width="330"
6868
Visibility="Visible" />
69+
70+
<StatusBar VerticalAlignment="Bottom" Name="StatusBar">
71+
<StatusBarItem>
72+
<TextBlock Name="TxtElapsedTime" Text="Elapsed Time: 0 ms"/>
73+
</StatusBarItem>
74+
</StatusBar>
6975
</Grid>
7076
</Window>

src/UiaXpathTester/MainWindow.xaml.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.Diagnostics;
45
using System.Linq;
56
using System.Runtime.InteropServices;
67
using System.Text.Json;
78
using System.Text.RegularExpressions;
8-
using System.Threading;
99
using System.Threading.Tasks;
1010
using System.Windows;
1111
using System.Xml.Linq;
@@ -29,11 +29,7 @@ public MainWindow()
2929
InitializeComponent();
3030
}
3131

32-
/// <summary>
33-
/// Event handler for the "Test XPath" button click.
34-
/// </summary>
35-
/// <param name="sender">The event sender.</param>
36-
/// <param name="e">The event arguments.</param>
32+
// Event handler for the "Test XPath" button click.
3733
private async void BtnTestXpath_Click(object sender, RoutedEventArgs e)
3834
{
3935
// Update UI to show "Working..."
@@ -43,28 +39,33 @@ await Dispatcher.BeginInvoke(new Action(() =>
4339
DtaElementData.Visibility = Visibility.Hidden;
4440
BtnTestXpath.IsEnabled = false;
4541
LblStatus.Content = "Working...";
42+
TxtElapsedTime.Text = "";
4643
}));
4744

45+
// Start the stopwatch to measure elapsed time
46+
var stopwatch = Stopwatch.StartNew();
47+
4848
// Perform the actual logic asynchronously
4949
await Task.Run(() => PublishDataGrid(mainWindow: this));
5050

51-
// Update UI to revert changes
51+
// Stop the stopwatch
52+
stopwatch.Stop();
53+
54+
// Update UI to revert changes and show elapsed time
5255
await Dispatcher.BeginInvoke(new Action(() =>
5356
{
5457
LblStatus.Visibility = Visibility.Hidden;
5558
DtaElementData.Visibility = Visibility.Visible;
5659
BtnTestXpath.IsEnabled = true;
60+
TxtElapsedTime.Text = $"Elapsed Time: {stopwatch.ElapsedMilliseconds} ms";
5761
}));
5862
}
5963

60-
/// <summary>
61-
/// Publishes data from a DataGrid using the specified XPath in the provided MainWindow.
62-
/// </summary>
63-
/// <param name="mainWindow">The MainWindow instance where the DataGrid and XPath TextBox are located.</param>
64-
private static void PublishDataGrid(MainWindow mainWindow)
64+
// Publishes data from a DataGrid using the specified XPath in the provided MainWindow.
65+
private static async Task PublishDataGrid(MainWindow mainWindow)
6566
{
6667
// Use the dispatcher to execute the operation on the UI thread.
67-
mainWindow.Dispatcher.BeginInvoke(() =>
68+
await mainWindow.Dispatcher.BeginInvoke(() =>
6869
{
6970
// Create a new UI Automation instance.
7071
var automation = new CUIAutomation8();

0 commit comments

Comments
 (0)