Skip to content

Commit a0b826e

Browse files
committed
add stop processing button, process in another thread
1 parent cb8768e commit a0b826e

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

MainWindow.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<TextBox x:Name="txtConsole" HorizontalAlignment="Left" Height="53" Margin="10,526,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="770" IsReadOnly="True" UndoLimit="1" Background="#FF404040" BorderBrush="{x:Null}" Foreground="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
6868
<Label x:Name="label_Copy5" Content="Generated commandline parameters:" HorizontalAlignment="Left" Margin="5,503,0,0" VerticalAlignment="Top" Foreground="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" FontWeight="Bold"/>
6969
<Button x:Name="btnGetParams" Content="Get Commandline params" HorizontalAlignment="Left" Margin="10,440,0,0" VerticalAlignment="Top" Width="214" Height="58" Click="btnGetParams_Click"/>
70+
<Grid x:Name="gridProcessingPanel" Background="#A3000000" Visibility="Hidden">
71+
<Button x:Name="btnCancel" Content="Stop processing!" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="214" Height="58" Click="BtnCancel_Click"/>
72+
</Grid>
7073

7174
</Grid>
7275
</Window>

MainWindow.xaml.cs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.IO;
99
using System.Runtime.InteropServices;
10+
using System.Threading;
1011
using System.Windows;
1112

1213
namespace PointCloudConverter
@@ -25,9 +26,14 @@ public partial class MainWindow : Window
2526
[DllImport("kernel32.dll", SetLastError = true)]
2627
static extern bool FreeConsole();
2728

29+
Thread workerThread;
30+
static bool abort = false;
31+
public static MainWindow mainWindowStatic;
32+
2833
public MainWindow()
2934
{
3035
InitializeComponent();
36+
mainWindowStatic = this;
3137
Main();
3238
}
3339

@@ -51,7 +57,11 @@ private void Main()
5157
var importSettings = ArgParser.Parse(null, rootFolder);
5258

5359
// if have files, process them
54-
if (importSettings != null) ProcessAllFiles(importSettings);
60+
if (importSettings != null)
61+
{
62+
// NOTE no background thread from commandline
63+
ProcessAllFiles(importSettings);
64+
}
5565

5666
// end output
5767
Console.WriteLine("Exit");
@@ -70,8 +80,11 @@ private void Main()
7080
}
7181

7282
// main processing loop
73-
private static void ProcessAllFiles(ImportSettings importSettings)
83+
//private static void ProcessAllFiles(ImportSettings importSettings)
84+
private static void ProcessAllFiles(System.Object importSettingsObject)
7485
{
86+
var importSettings = (ImportSettings)importSettingsObject;
87+
7588
Stopwatch stopwatch = new Stopwatch();
7689
stopwatch.Start();
7790

@@ -84,13 +97,25 @@ private static void ProcessAllFiles(ImportSettings importSettings)
8497
{
8598
Console.WriteLine("\nReading file (" + i + "/" + (len - 1) + ") : " + importSettings.inputFiles[i] + " (" + Tools.HumanReadableFileSize(new FileInfo(importSettings.inputFiles[i]).Length) + ")");
8699

100+
//if (abort==true)
101+
87102
// do actual point cloud parsing for this file
88103
ParseFile(importSettings, i);
89104
}
90105

91106
stopwatch.Stop();
92107
Console.WriteLine("Elapsed: " + (TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds)).ToString(@"hh\h\ mm\m\ ss\s\ ms\m\s"));
93108
stopwatch.Reset();
109+
110+
Application.Current.Dispatcher.Invoke(new Action(() =>
111+
{
112+
mainWindowStatic.HideProcessingPanel();
113+
}));
114+
}
115+
116+
void HideProcessingPanel()
117+
{
118+
gridProcessingPanel.Visibility = Visibility.Hidden;
94119
}
95120

96121
// process single file
@@ -218,9 +243,10 @@ private void btnConvert_Click(object sender, RoutedEventArgs e)
218243
{
219244
SaveSettings();
220245
StartProcess();
246+
gridProcessingPanel.Visibility = Visibility.Visible;
221247
}
222248

223-
void StartProcess(bool skipProcess = true)
249+
void StartProcess(bool doProcess = true)
224250
{
225251
// get args from GUI settings, TODO could directly create new import settings..
226252
var args = new List<string>();
@@ -267,13 +293,27 @@ void StartProcess(bool skipProcess = true)
267293
Console.WriteLine(cl);
268294

269295
// TODO lock UI, add cancel button, add progress bar
270-
if (skipProcess == true) ProcessAllFiles(importSettings);
296+
if (doProcess == true)
297+
{
298+
ParameterizedThreadStart start = new ParameterizedThreadStart(ProcessAllFiles);
299+
workerThread = new Thread(start);
300+
workerThread.IsBackground = true;
301+
workerThread.Start(importSettings);
302+
}
271303
}
272304
}
273305

306+
274307
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
275308
{
276309
SaveSettings();
310+
311+
abort = true;
312+
if (workerThread != null)
313+
{
314+
workerThread.Abort();
315+
Environment.Exit(Environment.ExitCode);
316+
}
277317
}
278318

279319
private void btnBrowseInput_Click(object sender, RoutedEventArgs e)
@@ -386,5 +426,15 @@ private void btnGetParams_Click(object sender, RoutedEventArgs e)
386426
private void Window_Loaded(object sender, RoutedEventArgs e)
387427
{
388428
}
429+
430+
private void BtnCancel_Click(object sender, RoutedEventArgs e)
431+
{
432+
abort = true;
433+
if (workerThread != null)
434+
{
435+
workerThread.Abort();
436+
Environment.Exit(Environment.ExitCode);
437+
}
438+
}
389439
} // class
390440
} // namespace

0 commit comments

Comments
 (0)