Skip to content

Commit 1fe33a3

Browse files
committed
Using asynchronous database operations in Closing event
1 parent f1112f0 commit 1fe33a3

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

IPConfig/ViewModels/MainViewModel.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.ComponentModel;
55
using System.Globalization;
66
using System.Linq;
7+
using System.Threading.Tasks;
78
using System.Windows;
89

910
using CommunityToolkit.Mvvm.ComponentModel;
@@ -65,7 +66,7 @@ private static void ChangeLanguage(string name)
6566
}
6667

6768
[RelayCommand]
68-
private void Closing(CancelEventArgs e)
69+
private async Task ClosingAsync(CancelEventArgs e)
6970
{
7071
if (App.IsDbSyncing)
7172
{
@@ -129,16 +130,23 @@ private void Closing(CancelEventArgs e)
129130
}
130131
}
131132

133+
e.Cancel = true;
134+
132135
var iPConfigList = Messenger.Send<RequestMessage<IEnumerable<EditableIPConfigModel>>, string>("IPConfigList").Response.ToImmutableArray();
133136

134-
LiteDbHelper.Handle(col => {
135-
for (int i = 0; i < iPConfigList.Length; i++)
136-
{
137-
var item = iPConfigList[i];
138-
item.Order = i;
139-
col.Update(item);
140-
}
137+
await Task.Run(() => {
138+
LiteDbHelper.Handle(col => {
139+
for (int i = 0; i < iPConfigList.Length; i++)
140+
{
141+
var item = iPConfigList[i];
142+
item.Order = i;
143+
col.Update(item);
144+
}
145+
});
141146
});
147+
148+
App.CanForceClose = true;
149+
App.Current.Shutdown();
142150
}
143151

144152
[RelayCommand]

0 commit comments

Comments
 (0)