Skip to content

Commit dd62ca4

Browse files
committed
Improved STATask
1 parent c525ec9 commit dd62ca4

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

src/Files.App.CsWin32/ManualGuid.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public static Guid* IID_IStorageProviderStatusUISourceFactory
5757
[GuidRVAGen.Guid("1C9CD5BB-98E9-4491-A60F-31AACC72B83C")]
5858
public static partial Guid* IID_IObjectWithSelection { get; }
5959

60-
[GuidRVAGen.Guid("BCC18B79-BA16-442F-80C4-8A59C30C463B")]
61-
public static partial Guid* IID_IShellItemImageFactory { get; }
62-
6360
[GuidRVAGen.Guid("000214E8-0000-0000-C000-000000000046")]
6461
public static partial Guid* IID_IShellExtInit { get; }
6562

src/Files.App.Storage/Storables/WindowsStorage/IWindowsStorable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace Files.App.Storage
77
{
88
public unsafe interface IWindowsStorable : IStorableChild, IEquatable<IWindowsStorable>, IDisposable
99
{
10-
IShellItem* ThisPtr { get; }
10+
IShellItem* ThisPtr { get; set; }
1111

12-
IContextMenu* ContextMenu { get; }
12+
IContextMenu* ContextMenu { get; set; }
1313
}
1414
}

src/Files.App.Storage/Storables/WindowsStorage/STATask.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Microsoft.Extensions.Logging;
45
using Windows.Win32;
56

67
namespace Files.App.Storage
78
{
89
/// <summary>
9-
/// Represents an asynchronous operation on STA.
10+
/// Represents a synchronous/asynchronous operation on STA.
1011
/// </summary>
1112
public partial class STATask
1213
{
13-
public static Task Run(Action action)
14+
public static Task Run(Action action, ILogger? logger = null)
1415
{
1516
var tcs = new TaskCompletionSource();
1617

@@ -26,25 +27,24 @@ public static Task Run(Action action)
2627
}
2728
catch (Exception ex)
2829
{
30+
tcs.SetResult();
31+
logger?.LogWarning(ex, "An exception was occurred during the execution within STA.");
2932
tcs.SetException(ex);
3033
}
3134
finally
3235
{
3336
PInvoke.OleUninitialize();
3437
}
35-
})
36-
{
37-
IsBackground = true,
38-
Priority = ThreadPriority.Normal
39-
};
38+
});
4039

40+
thread.IsBackground = true;
4141
thread.SetApartmentState(ApartmentState.STA);
4242
thread.Start();
4343

4444
return tcs.Task;
4545
}
4646

47-
public static Task<T> Run<T>(Func<T> func)
47+
public static Task<T> Run<T>(Func<T> func, ILogger? logger = null)
4848
{
4949
var tcs = new TaskCompletionSource<T>();
5050

@@ -59,25 +59,24 @@ public static Task<T> Run<T>(Func<T> func)
5959
}
6060
catch (Exception ex)
6161
{
62+
tcs.SetResult(default!);
63+
logger?.LogWarning(ex, "An exception was occurred during the execution within STA.");
6264
tcs.SetException(ex);
6365
}
6466
finally
6567
{
6668
PInvoke.OleUninitialize();
6769
}
68-
})
69-
{
70-
IsBackground = true,
71-
Priority = ThreadPriority.Normal
72-
};
70+
});
7371

72+
thread.IsBackground = true;
7473
thread.SetApartmentState(ApartmentState.STA);
7574
thread.Start();
7675

7776
return tcs.Task;
7877
}
7978

80-
public static Task Run(Func<Task> func)
79+
public static Task Run(Func<Task> func, ILogger? logger = null)
8180
{
8281
var tcs = new TaskCompletionSource();
8382

@@ -93,25 +92,24 @@ public static Task Run(Func<Task> func)
9392
}
9493
catch (Exception ex)
9594
{
95+
tcs.SetResult();
96+
logger?.LogWarning(ex, "An exception was occurred during the execution within STA.");
9697
tcs.SetException(ex);
9798
}
9899
finally
99100
{
100101
PInvoke.OleUninitialize();
101102
}
102-
})
103-
{
104-
IsBackground = true,
105-
Priority = ThreadPriority.Normal
106-
};
103+
});
107104

105+
thread.IsBackground = true;
108106
thread.SetApartmentState(ApartmentState.STA);
109107
thread.Start();
110108

111109
return tcs.Task;
112110
}
113111

114-
public static Task<T?> Run<T>(Func<Task<T>> func)
112+
public static Task<T?> Run<T>(Func<Task<T>> func, ILogger? logger = null)
115113
{
116114
var tcs = new TaskCompletionSource<T?>();
117115

@@ -126,18 +124,17 @@ public static Task Run(Func<Task> func)
126124
}
127125
catch (Exception ex)
128126
{
127+
tcs.SetResult(default);
128+
logger?.LogWarning(ex, "An exception was occurred during the execution within STA.");
129129
tcs.SetException(ex);
130130
}
131131
finally
132132
{
133133
PInvoke.OleUninitialize();
134134
}
135-
})
136-
{
137-
IsBackground = true,
138-
Priority = ThreadPriority.Normal
139-
};
135+
});
140136

137+
thread.IsBackground = true;
141138
thread.SetApartmentState(ApartmentState.STA);
142139
thread.Start();
143140

src/Files.App.Storage/Storables/WindowsStorage/WindowsFolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public IContextMenu* ShellNewMenu
1919
get;
2020

2121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
protected internal set;
22+
set;
2323
}
2424

2525
public WindowsFolder(IShellItem* ptr)

src/Files.App.Storage/Storables/WindowsStorage/WindowsStorable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public IShellItem* ThisPtr
1717
get;
1818

1919
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20-
protected internal set;
20+
set;
2121
}
2222

2323
public IContextMenu* ContextMenu
@@ -26,7 +26,7 @@ public IContextMenu* ContextMenu
2626
get;
2727

2828
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
protected internal set;
29+
set;
3030
}
3131

3232
public string Id => this.GetDisplayName(SIGDN.SIGDN_FILESYSPATH);
@@ -49,7 +49,7 @@ public IContextMenu* ContextMenu
4949

5050
public static WindowsStorable? TryParse(IShellItem* pShellItem)
5151
{
52-
bool isFolder = pShellItem->GetAttributes(SFGAO_FLAGS.SFGAO_FOLDER, out var returnedAttributes).Succeeded && returnedAttributes == SFGAO_FLAGS.SFGAO_FOLDER;
52+
bool isFolder = pShellItem->GetAttributes(SFGAO_FLAGS.SFGAO_FOLDER, out var returnedAttributes).Succeeded && returnedAttributes is SFGAO_FLAGS.SFGAO_FOLDER;
5353

5454
return isFolder ? new WindowsFolder(pShellItem) : new WindowsFile(pShellItem);
5555
}

0 commit comments

Comments
 (0)