Skip to content

Commit 32d91d8

Browse files
committed
Process ftp messages in parallel
1 parent d64042b commit 32d91d8

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

Files.Launcher/MessageHandlers/Win32MessageHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task ParseArgumentsAsync(PipeStream connection, Dictionary<string,
6262
var fileIconPath = (string)message["filePath"];
6363
var thumbnailSize = (int)(long)message["thumbnailSize"];
6464
var isOverlayOnly = (bool)message["isOverlayOnly"];
65-
var iconOverlay = Win32API.StartSTATask(() => Win32API.GetFileIconAndOverlay(fileIconPath, thumbnailSize, true, isOverlayOnly)).Result;
65+
var iconOverlay = await Win32API.StartSTATask(() => Win32API.GetFileIconAndOverlay(fileIconPath, thumbnailSize, true, isOverlayOnly));
6666
await Win32API.SendMessageAsync(connection, new ValueSet()
6767
{
6868
{ "Icon", iconOverlay.icon },
@@ -73,7 +73,7 @@ public async Task ParseArgumentsAsync(PipeStream connection, Dictionary<string,
7373
case "GetIconWithoutOverlay":
7474
var fileIconPath2 = (string)message["filePath"];
7575
var thumbnailSize2 = (int)(long)message["thumbnailSize"];
76-
var icon2 = Win32API.StartSTATask(() => Win32API.GetFileIconAndOverlay(fileIconPath2, thumbnailSize2, false)).Result;
76+
var icon2 = await Win32API.StartSTATask(() => Win32API.GetFileIconAndOverlay(fileIconPath2, thumbnailSize2, false));
7777
await Win32API.SendMessageAsync(connection, new ValueSet()
7878
{
7979
{ "Icon", icon2.icon },

Files.Launcher/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private static void EndReadCallBack(IAsyncResult result)
157157
{
158158
var message = info.Message.ToString().TrimEnd('\0');
159159

160-
Connection_RequestReceived(connection, JsonConvert.DeserializeObject<Dictionary<string, object>>(message));
160+
_ = Task.Run(() => Connection_RequestReceived(connection, JsonConvert.DeserializeObject<Dictionary<string, object>>(message)));
161161

162162
// Begin a new reading operation
163163
var nextInfo = (Buffer: new byte[connection.InBufferSize], Message: new StringBuilder());
@@ -170,7 +170,7 @@ private static void EndReadCallBack(IAsyncResult result)
170170
}
171171
}
172172

173-
private static async void Connection_RequestReceived(PipeStream conn, Dictionary<string, object> message)
173+
private static async Task Connection_RequestReceived(PipeStream conn, Dictionary<string, object> message)
174174
{
175175
// Get a deferral because we use an awaitable API below to respond to the message
176176
// and we don't want this call to get cancelled while we are waiting.
@@ -187,7 +187,7 @@ private static async void Connection_RequestReceived(PipeStream conn, Dictionary
187187
var arguments = (string)message["Arguments"];
188188
Logger.Info($"Argument: {arguments}");
189189

190-
await ParseArgumentsAsync(message, arguments);
190+
await Extensions.IgnoreExceptions(() => ParseArgumentsAsync(message, arguments), Logger);
191191
}
192192
}
193193

Files.Launcher/Win32API.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public static string[] CommandLineToArgs(string commandLine)
157157
}
158158
}
159159

160+
private static object lockObject = new object();
161+
160162
public static (string icon, string overlay) GetFileIconAndOverlay(string path, int thumbnailSize, bool getOverlay = true, bool onlyGetOverlay = false)
161163
{
162164
string iconStr = null, overlayStr = null;
@@ -197,24 +199,31 @@ public static (string icon, string overlay) GetFileIconAndOverlay(string path, i
197199
}
198200

199201
User32.DestroyIcon(shfi.hIcon);
200-
Shell32.SHGetImageList(Shell32.SHIL.SHIL_LARGE, typeof(ComCtl32.IImageList).GUID, out var tmp);
201-
using var imageList = ComCtl32.SafeHIMAGELIST.FromIImageList(tmp);
202-
if (imageList.IsNull || imageList.IsInvalid)
203-
{
204-
return (iconStr, null);
205-
}
206202

207-
var overlayIdx = shfi.iIcon >> 24;
208-
if (overlayIdx != 0)
203+
lock (lockObject)
209204
{
210-
var overlayImage = imageList.Interface.GetOverlayImage(overlayIdx);
211-
using var hOverlay = imageList.Interface.GetIcon(overlayImage, ComCtl32.IMAGELISTDRAWFLAGS.ILD_TRANSPARENT);
212-
if (!hOverlay.IsNull && !hOverlay.IsInvalid)
205+
if (!Shell32.SHGetImageList(Shell32.SHIL.SHIL_LARGE, typeof(ComCtl32.IImageList).GUID, out var imageList).Succeeded)
213206
{
214-
using var image = hOverlay.ToIcon().ToBitmap();
215-
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
216-
overlayStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
207+
return (iconStr, null);
217208
}
209+
210+
var overlayIdx = shfi.iIcon >> 24;
211+
if (overlayIdx != 0)
212+
{
213+
var overlayImage = imageList.GetOverlayImage(overlayIdx);
214+
using var hOverlay = imageList.GetIcon(overlayImage, ComCtl32.IMAGELISTDRAWFLAGS.ILD_TRANSPARENT);
215+
if (!hOverlay.IsNull && !hOverlay.IsInvalid)
216+
{
217+
using (var icon = hOverlay.ToIcon())
218+
using (var image = icon.ToBitmap())
219+
{
220+
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
221+
overlayStr = Convert.ToBase64String(bitmapData, 0, bitmapData.Length);
222+
}
223+
}
224+
}
225+
226+
Marshal.ReleaseComObject(imageList);
218227
}
219228

220229
return (iconStr, overlayStr);

0 commit comments

Comments
 (0)