diff --git a/bin/shell-x.exe b/bin/shell-x.exe index 87f04ed..8e67ad7 100644 Binary files a/bin/shell-x.exe and b/bin/shell-x.exe differ diff --git a/shell-x/Program.cs b/shell-x/Program.cs index b1361fc..548e8ac 100644 --- a/shell-x/Program.cs +++ b/shell-x/Program.cs @@ -253,9 +253,34 @@ protected override ContextMenuStrip CreateMenu() var menu = new ContextMenuStrip(); menu.Items.AddRange(items); + + // DisposeLastMenu(); + // lastOpenedMenu = menu; + return menu; } + static ContextMenuStrip lastOpenedMenu; + + void DisposeLastMenu() + { + if (lastOpenedMenu != null) + { + foreach (var item in lastOpenedMenu.Items.OfType()) + { + try + { + var img = item.Image; + item.Image = null; + img?.Dispose(); + } + catch { } + } + // lastOpenedMenu.Close(); + // lastOpenedMenu.Dispose(); + } + } + static Image LookupImageFor(string path) { return LookupImageFor(path, ".ico") ?? @@ -273,8 +298,6 @@ static Image LookupImageFor(string path, string imgExtension) return img; } - static List LoadedImages = new List(); - internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArguments) { var menus = new List(); @@ -306,7 +329,7 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg try { var size = parentMenu.ContentRectangle.Height.ToStandardIconSize(); - parentMenu.Image = parentMenu.Image?.Resize(size, size); + parentMenu.Image = parentMenu.Image?.Resize(size, size, dispose: true); } catch { } @@ -334,7 +357,8 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg try { var size = menu.ContentRectangle.Height.ToStandardIconSize(); - menu.Image = menu.Image?.Resize(size, size); + + menu.Image = menu.Image?.Resize(size, size, dispose: true); } catch { } @@ -353,21 +377,23 @@ internal static ToolStripItem[] BuildMenuFrom(string configDir, string invokeArg public static void Cleanup() { - LoadedImages.ForEach(i => i.Dispose()); - - var except = Process.GetCurrentProcess().Id.ToString(); - Directory.GetFiles(App.ConfigDir.PathJoin(".run").EnsureDirectory(), "*.*.ps1") - .Select(x => new { path = x, pid = x.GetFileName().Split('.').First() }) - .Where(x => x.pid != except) - .ToList() - .ForEach(x => - { - try + try + { + var except = Process.GetCurrentProcess().Id.ToString(); + Directory.GetFiles(App.ConfigDir.PathJoin(".run").EnsureDirectory(), "*.*.ps1") + .Select(x => new { path = x, pid = x.GetFileName().Split('.').First() }) + .Where(x => x.pid != except) + .ToList() + .ForEach(x => { - File.Delete(x.path); - } - catch { } - }); + try + { + File.Delete(x.path); + } + catch { } + }); + } + catch { } } public static string CloneScript(string script) @@ -487,18 +513,33 @@ string GetConfigDirFor(string file) static class Utils { + static Dictionary loadedImages = new Dictionary(); + public static Image ReadImage(this string file) { + var imageId = $"{file}{File.GetLastWriteTimeUtc(file).ToFileTimeUtc()}"; + + if (loadedImages.ContainsKey(imageId)) + return loadedImages[imageId]; + using (var ms = new MemoryStream(File.ReadAllBytes(file))) { + Image image; if (string.Compare(Path.GetExtension(file), ".ico", StringComparison.OrdinalIgnoreCase) == 0) - return new Icon(ms).ToBitmap(); + { + using (var temp = new Icon(ms)) + image = temp.ToBitmap(); + } else - return Image.FromStream(ms); + { + image = Image.FromStream(ms); + } + loadedImages[imageId] = image; + return image; } } - public static Bitmap Resize(this Image image, int width, int height) + public static Bitmap Resize(this Image image, int width, int height, bool dispose) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); @@ -520,6 +561,8 @@ public static Bitmap Resize(this Image image, int width, int height) } } + if (dispose) + image.Dispose(); return destImage; } } \ No newline at end of file