@@ -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
0 commit comments