Skip to content

Commit 5ac364a

Browse files
committed
Init
1 parent 7bd2dde commit 5ac364a

File tree

9 files changed

+44
-181
lines changed

9 files changed

+44
-181
lines changed

src/Files.App/Helpers/Win32/Win32Helper.Shell.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static partial class Win32Helper
2424
path = $"shell:{path}";
2525
}
2626

27-
return await Win32Helper.StartSTATask(() =>
27+
return await STATask.Run(() =>
2828
{
2929
var flc = new List<ShellFileItem>();
3030
var folder = (ShellFileItem)null;
@@ -77,7 +77,7 @@ public static partial class Win32Helper
7777
}
7878

7979
return (folder, flc);
80-
});
80+
}, App.Logger);
8181
}
8282

8383
public static string GetFolderFromKnownFolderGUID(Guid guid)

src/Files.App/Helpers/Win32/Win32Helper.Storage.cs

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -27,143 +27,6 @@ namespace Files.App.Helpers
2727
/// </summary>
2828
public static partial class Win32Helper
2929
{
30-
public static Task StartSTATask(Func<Task> func)
31-
{
32-
var taskCompletionSource = new TaskCompletionSource();
33-
Thread thread = new Thread(async () =>
34-
{
35-
Ole32.OleInitialize();
36-
37-
try
38-
{
39-
await func();
40-
taskCompletionSource.SetResult();
41-
}
42-
catch (Exception ex)
43-
{
44-
taskCompletionSource.SetResult();
45-
App.Logger.LogWarning(ex, ex.Message);
46-
}
47-
finally
48-
{
49-
Ole32.OleUninitialize();
50-
}
51-
})
52-
53-
{
54-
IsBackground = true,
55-
Priority = ThreadPriority.Normal
56-
};
57-
58-
thread.SetApartmentState(ApartmentState.STA);
59-
thread.Start();
60-
61-
return taskCompletionSource.Task;
62-
}
63-
64-
public static Task StartSTATask(Action action)
65-
{
66-
var taskCompletionSource = new TaskCompletionSource();
67-
Thread thread = new Thread(() =>
68-
{
69-
Ole32.OleInitialize();
70-
71-
try
72-
{
73-
action();
74-
taskCompletionSource.SetResult();
75-
}
76-
catch (Exception ex)
77-
{
78-
taskCompletionSource.SetResult();
79-
App.Logger.LogWarning(ex, ex.Message);
80-
}
81-
finally
82-
{
83-
Ole32.OleUninitialize();
84-
}
85-
})
86-
87-
{
88-
IsBackground = true,
89-
Priority = ThreadPriority.Normal
90-
};
91-
92-
thread.SetApartmentState(ApartmentState.STA);
93-
thread.Start();
94-
95-
return taskCompletionSource.Task;
96-
}
97-
98-
public static Task<T?> StartSTATask<T>(Func<T> func)
99-
{
100-
var taskCompletionSource = new TaskCompletionSource<T?>();
101-
102-
Thread thread = new Thread(() =>
103-
{
104-
Ole32.OleInitialize();
105-
106-
try
107-
{
108-
taskCompletionSource.SetResult(func());
109-
}
110-
catch (Exception ex)
111-
{
112-
taskCompletionSource.SetResult(default);
113-
App.Logger.LogWarning(ex, ex.Message);
114-
//tcs.SetException(e);
115-
}
116-
finally
117-
{
118-
Ole32.OleUninitialize();
119-
}
120-
})
121-
122-
{
123-
IsBackground = true,
124-
Priority = ThreadPriority.Normal
125-
};
126-
127-
thread.SetApartmentState(ApartmentState.STA);
128-
thread.Start();
129-
130-
return taskCompletionSource.Task;
131-
}
132-
133-
public static Task<T?> StartSTATask<T>(Func<Task<T>> func)
134-
{
135-
var taskCompletionSource = new TaskCompletionSource<T?>();
136-
137-
Thread thread = new Thread(async () =>
138-
{
139-
Ole32.OleInitialize();
140-
try
141-
{
142-
taskCompletionSource.SetResult(await func());
143-
}
144-
catch (Exception ex)
145-
{
146-
taskCompletionSource.SetResult(default);
147-
App.Logger.LogInformation(ex, ex.Message);
148-
//tcs.SetException(e);
149-
}
150-
finally
151-
{
152-
Ole32.OleUninitialize();
153-
}
154-
})
155-
156-
{
157-
IsBackground = true,
158-
Priority = ThreadPriority.Normal
159-
};
160-
161-
thread.SetApartmentState(ApartmentState.STA);
162-
thread.Start();
163-
164-
return taskCompletionSource.Task;
165-
}
166-
16730
public static async Task<string?> GetDefaultFileAssociationAsync(string filename, bool checkDesktopFirst = true)
16831
{
16932
// check if there exists an user choice first

src/Files.App/Services/Storage/StorageNetworkService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task<IEnumerable<IFolder>> GetComputersAsync()
9999
/// <inheritdoc/>
100100
public async Task<IEnumerable<IFolder>> GetShortcutsAsync()
101101
{
102-
var networkLocations = await Win32Helper.StartSTATask(() =>
102+
var networkLocations = await STATask.Run(() =>
103103
{
104104
var locations = new List<ShellLinkItem>();
105105
using (var netHood = new ShellFolder(Shell32.KNOWNFOLDERID.FOLDERID_NetHood))
@@ -122,7 +122,7 @@ public async Task<IEnumerable<IFolder>> GetShortcutsAsync()
122122
}
123123
}
124124
return locations;
125-
});
125+
}, App.Logger);
126126

127127
return (networkLocations ?? Enumerable.Empty<ShellLinkItem>()).Select(item =>
128128
{
@@ -200,13 +200,13 @@ public bool DisconnectNetworkDrive(IFolder drive)
200200
/// <inheritdoc/>
201201
public Task OpenMapNetworkDriveDialogAsync()
202202
{
203-
return Win32Helper.StartSTATask(() =>
203+
return STATask.Run(() =>
204204
{
205205
return CommonDialogService.Open_NetworkConnectionDialog(
206206
MainWindow.Instance.WindowHandle,
207207
useMostRecentPath: true,
208208
hideRestoreConnectionCheckBox: false);
209-
});
209+
}, App.Logger);
210210
}
211211

212212
/// <inheritdoc/>

src/Files.App/Services/Storage/StorageTrashBinService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public bool EmptyTrashBin()
8383
/// <inheritdoc/>
8484
public async Task<bool> RestoreAllTrashesAsync()
8585
{
86-
return await Win32Helper.StartSTATask(() =>
86+
return await STATask.Run(() =>
8787
{
8888
try
8989
{
@@ -95,7 +95,7 @@ public async Task<bool> RestoreAllTrashesAsync()
9595
{
9696
return false;
9797
}
98-
});
98+
}, App.Logger);
9999
}
100100

101101
private unsafe bool RestoreAllTrashesInternal()

src/Files.App/Services/Windows/WindowsQuickAccessService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ private async Task UnpinFromSidebarAsync(string[] folderPaths, bool doUpdateQuic
6363
(folderPaths.Contains(path) ||
6464
(path.StartsWith(@"\\SHELL\\") && folderPaths.Any(x => x.StartsWith(@"\\SHELL\\")))))
6565
{
66-
await Win32Helper.StartSTATask(async () =>
66+
await STATask.Run(async () =>
6767
{
6868
fi.InvokeVerb("unpinfromhome");
69-
});
69+
}, App.Logger);
7070
continue;
7171
}
7272
}
7373

7474
if (folderPaths.Contains(pathStr))
7575
{
76-
await Win32Helper.StartSTATask(async () =>
76+
await STATask.Run(async () =>
7777
{
7878
fi.InvokeVerb("unpinfromhome");
79-
});
79+
}, App.Logger);
8080
}
8181
}
8282

src/Files.App/Utils/Library/LibraryManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void InitializeWatcher()
6767
/// <returns>List of library items</returns>
6868
public static async Task<List<LibraryLocationItem>> ListUserLibraries()
6969
{
70-
var libraries = await Win32Helper.StartSTATask(() =>
70+
var libraries = await STATask.Run(() =>
7171
{
7272
try
7373
{
@@ -90,7 +90,7 @@ public static async Task<List<LibraryLocationItem>> ListUserLibraries()
9090
}
9191

9292
return [];
93-
});
93+
}, App.Logger);
9494

9595
return libraries.Select(lib => new LibraryLocationItem(lib)).ToList();
9696
}
@@ -134,7 +134,7 @@ public async Task<bool> CreateNewLibrary(string name)
134134
if (string.IsNullOrWhiteSpace(name) || !CanCreateLibrary(name).result)
135135
return false;
136136

137-
var newLib = new LibraryLocationItem(await Win32Helper.StartSTATask(() =>
137+
var newLib = new LibraryLocationItem(await STATask.Run(() =>
138138
{
139139
try
140140
{
@@ -150,7 +150,7 @@ public async Task<bool> CreateNewLibrary(string name)
150150
}
151151

152152
return Task.FromResult<ShellLibraryItem>(null);
153-
}));
153+
}, App.Logger));
154154

155155
if (newLib is not null)
156156
{
@@ -178,7 +178,7 @@ public async Task<LibraryLocationItem> UpdateLibrary(string libraryPath, string
178178
// Nothing to update
179179
return null;
180180

181-
var item = await Win32Helper.StartSTATask(() =>
181+
var item = await STATask.Run(() =>
182182
{
183183
try
184184
{
@@ -231,7 +231,7 @@ public async Task<LibraryLocationItem> UpdateLibrary(string libraryPath, string
231231
}
232232

233233
return Task.FromResult<ShellLibraryItem>(null);
234-
});
234+
}, App.Logger);
235235

236236
var newLib = item is not null ? new LibraryLocationItem(item) : null;
237237
if (newLib is not null)

src/Files.App/Utils/Shell/LaunchHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private static async Task<bool> HandleApplicationLaunch(string application, stri
141141
{
142142
try
143143
{
144-
var opened = await Win32Helper.StartSTATask(async () =>
144+
var opened = await STATask.Run(async () =>
145145
{
146146
var split = application.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x));
147147
if (split.Count() == 1)
@@ -171,21 +171,21 @@ private static async Task<bool> HandleApplicationLaunch(string application, stri
171171
}
172172

173173
return true;
174-
});
174+
}, App.Logger);
175175

176176
if (!opened)
177177
{
178178
if (application.StartsWith(@"\\SHELL\", StringComparison.Ordinal))
179179
{
180-
opened = await Win32Helper.StartSTATask(async () =>
180+
opened = await STATask.Run(async () =>
181181
{
182182
using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { application }, PInvoke.CMF_DEFAULTONLY);
183183

184184
if (cMenu is not null)
185185
await cMenu.InvokeItem(cMenu.Items.FirstOrDefault()?.ID ?? -1);
186186

187187
return true;
188-
});
188+
}, App.Logger);
189189
}
190190
}
191191

src/Files.App/Utils/Storage/Helpers/FileThumbnailHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class FileThumbnailHelper
1616
// Ensure size is at least 1 to prevent layout errors
1717
size = Math.Max(1, size);
1818

19-
return await Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
19+
return await STATask.Run(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions), App.Logger);
2020
}
2121

2222
/// <summary>
@@ -26,7 +26,7 @@ public static class FileThumbnailHelper
2626
/// <param name="isFolder"></param>
2727
/// <returns></returns>
2828
public static async Task<byte[]?> GetIconOverlayAsync(string path, bool isFolder)
29-
=> await Win32Helper.StartSTATask(() => Win32Helper.GetIconOverlay(path, isFolder));
29+
=> await STATask.Run(() => Win32Helper.GetIconOverlay(path, isFolder), App.Logger);
3030

3131
[Obsolete]
3232
public static async Task<byte[]?> LoadIconFromPathAsync(string filePath, uint thumbnailSize, ThumbnailMode thumbnailMode, ThumbnailOptions thumbnailOptions, bool isFolder = false)

0 commit comments

Comments
 (0)