Skip to content

Commit 6b35a21

Browse files
committed
Revert "Code Quality: Replaced the StartSTA helpers with STATask.Run (#17377)"
This reverts commit c1d9e84.
1 parent 1cf16fc commit 6b35a21

File tree

9 files changed

+181
-44
lines changed

9 files changed

+181
-44
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 STATask.Run(() =>
27+
return await Win32Helper.StartSTATask(() =>
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-
}, App.Logger);
80+
});
8181
}
8282

8383
public static string GetFolderFromKnownFolderGUID(Guid guid)

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

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async Task<IEnumerable<IFolder>> GetComputersAsync()
9191
/// <inheritdoc/>
9292
public async Task<IEnumerable<IFolder>> GetShortcutsAsync()
9393
{
94-
var networkLocations = await STATask.Run(() =>
94+
var networkLocations = await Win32Helper.StartSTATask(() =>
9595
{
9696
var locations = new List<ShellLinkItem>();
9797
using (var netHood = new ShellFolder(Shell32.KNOWNFOLDERID.FOLDERID_NetHood))
@@ -114,7 +114,7 @@ public async Task<IEnumerable<IFolder>> GetShortcutsAsync()
114114
}
115115
}
116116
return locations;
117-
}, App.Logger);
117+
});
118118

119119
return (networkLocations ?? Enumerable.Empty<ShellLinkItem>()).Select(item =>
120120
{
@@ -192,13 +192,13 @@ public bool DisconnectNetworkDrive(IFolder drive)
192192
/// <inheritdoc/>
193193
public Task OpenMapNetworkDriveDialogAsync()
194194
{
195-
return STATask.Run(() =>
195+
return Win32Helper.StartSTATask(() =>
196196
{
197197
return CommonDialogService.Open_NetworkConnectionDialog(
198198
MainWindow.Instance.WindowHandle,
199199
useMostRecentPath: true,
200200
hideRestoreConnectionCheckBox: false);
201-
}, App.Logger);
201+
});
202202
}
203203

204204
/// <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 STATask.Run(() =>
86+
return await Win32Helper.StartSTATask(() =>
8787
{
8888
try
8989
{
@@ -95,7 +95,7 @@ public async Task<bool> RestoreAllTrashesAsync()
9595
{
9696
return false;
9797
}
98-
}, App.Logger);
98+
});
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 STATask.Run(async () =>
66+
await Win32Helper.StartSTATask(async () =>
6767
{
6868
fi.InvokeVerb("unpinfromhome");
69-
}, App.Logger);
69+
});
7070
continue;
7171
}
7272
}
7373

7474
if (folderPaths.Contains(pathStr))
7575
{
76-
await STATask.Run(async () =>
76+
await Win32Helper.StartSTATask(async () =>
7777
{
7878
fi.InvokeVerb("unpinfromhome");
79-
}, App.Logger);
79+
});
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 STATask.Run(() =>
70+
var libraries = await Win32Helper.StartSTATask(() =>
7171
{
7272
try
7373
{
@@ -90,7 +90,7 @@ public static async Task<List<LibraryLocationItem>> ListUserLibraries()
9090
}
9191

9292
return [];
93-
}, App.Logger);
93+
});
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 STATask.Run(() =>
137+
var newLib = new LibraryLocationItem(await Win32Helper.StartSTATask(() =>
138138
{
139139
try
140140
{
@@ -150,7 +150,7 @@ public async Task<bool> CreateNewLibrary(string name)
150150
}
151151

152152
return Task.FromResult<ShellLibraryItem>(null);
153-
}, App.Logger));
153+
}));
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 STATask.Run(() =>
181+
var item = await Win32Helper.StartSTATask(() =>
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-
}, App.Logger);
234+
});
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 STATask.Run(async () =>
144+
var opened = await Win32Helper.StartSTATask(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-
}, App.Logger);
174+
});
175175

176176
if (!opened)
177177
{
178178
if (application.StartsWith(@"\\SHELL\", StringComparison.Ordinal))
179179
{
180-
opened = await STATask.Run(async () =>
180+
opened = await Win32Helper.StartSTATask(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-
}, App.Logger);
188+
});
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
@@ -14,7 +14,7 @@ public static class FileThumbnailHelper
1414
{
1515
var size = iconOptions.HasFlag(IconOptions.UseCurrentScale) ? requestedSize * App.AppModel.AppWindowDPI : requestedSize;
1616

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

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

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

0 commit comments

Comments
 (0)