Skip to content

Commit 2aaaf6f

Browse files
committed
Add a crummy dispose hack
Replace Thread with Task in recycle bin updater
1 parent 9977ed1 commit 2aaaf6f

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

WinDirStat.Net.Wpf.Base/ViewModel/MainViewModel.Commands.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Toolkit.Mvvm.Input;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using System.Windows;
56
using WinDirStat.Net.Model.Drives;
67
using WinDirStat.Net.Model.Files;
@@ -137,7 +138,7 @@ private void OnClose() {
137138
private void OnCancel() {
138139
Scanning.Cancel(false);
139140
}
140-
private void OnElevate() {
141+
private async void OnElevate() {
141142
// Dialog warn progress will be lost.
142143
// If yes, close then start new process.
143144
MessageBoxResult result = MessageBoxResult.Yes;
@@ -150,16 +151,16 @@ private void OnElevate() {
150151
try {
151152
OS.StartNewElevated();
152153
UI.Shutdown();
153-
Dispose();
154+
await DisposeAsync();
154155
}
155156
catch {
156157
Dialogs.ShowError(WindowOwner, "Failed to start elevated process!", "Error");
157158
}
158159
}
159160
}
160-
private void OnExit() {
161+
private async void OnExit() {
161162
UI.Shutdown();
162-
Dispose();
163+
await DisposeAsync();
163164
}
164165

165166
#endregion

WinDirStat.Net.Wpf.Base/ViewModel/MainViewModel.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Linq;
77
using System.Threading;
8+
using System.Threading.Tasks;
89
using WinDirStat.Net.Model.Files;
910
using WinDirStat.Net.Rendering;
1011
using WinDirStat.Net.Services;
@@ -15,7 +16,7 @@
1516

1617
namespace WinDirStat.Net.ViewModel {
1718
/// <summary>The main view model for the program.</summary>
18-
public partial class MainViewModel : ViewModelWindow {
19+
public partial class MainViewModel : ViewModelWindow, IAsyncDisposable {
1920

2021
#region Fields
2122

@@ -51,12 +52,12 @@ public partial class MainViewModel : ViewModelWindow {
5152

5253
private bool suppressRefresh;
5354

54-
private Thread recycleInfoThread;
55-
56-
private readonly object recycleLock = new object();
55+
private Task recycleInfoTask;
5756

5857
private Stopwatch lastRecycleWatch;
5958

59+
private bool disposed;
60+
6061
#endregion
6162

6263
#region Constructors
@@ -367,15 +368,23 @@ private void OnSelectedFilesChanged(object sender, NotifyCollectionChangedEventA
367368

368369
#endregion
369370

370-
#region IDisposable Implementation
371+
#region IAsyncDisposable Implementation
372+
373+
public async ValueTask DisposeAsync() {
374+
if (disposed)
375+
return;
371376

372-
public void Dispose() {
373-
using (Scanning) {
377+
disposed = true;
378+
await using (Scanning) {
374379
statusTimer.Stop();
375380
ramTimer.Stop();
376381
}
382+
383+
GC.SuppressFinalize(this);
377384
}
378385

386+
public async void Dispose() => await DisposeAsync();
387+
379388
#endregion
380389

381390
}

0 commit comments

Comments
 (0)