#NewFeature1 Increase performance by iconcache

#NewFeature2 Ask user to send exception by standard mail and restart
This commit is contained in:
Markus Hofknecht 2020-03-15 10:27:05 +01:00
parent fff542e91c
commit 0bdbfa3519
3 changed files with 74 additions and 11 deletions

View file

@ -84,7 +84,7 @@ namespace SystemTrayMenu.Controls
{
try
{
Icon = IconReader.GetFileIcon(TargetFilePath, false);
Icon = IconReader.GetFileIconWithCache(TargetFilePath, false);
// other project -> fails sometimes
//icon = IconHelper.ExtractIcon(TargetFilePath, 0);
@ -176,7 +176,7 @@ namespace SystemTrayMenu.Controls
}
else
{
Icon = IconReader.GetFileIcon(browserPath, false);
Icon = IconReader.GetFileIconWithCache(browserPath, false);
handled = true;
}
}

View file

@ -1,12 +1,15 @@
using Clearcove.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
namespace SystemTrayMenu.Helper
{
// from https://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using
// orig from https://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using
// added ImageList_GetIcon
// added IconCache
/// <summary>
/// Provides static methods to read system icons for both folders and files.
@ -16,6 +19,8 @@ namespace SystemTrayMenu.Helper
/// </example>
public class IconReader
{
public static Dictionary<string, Icon> dictIconCache = new Dictionary<string, Icon>();
/// <summary>
/// Options to specify the size of icons to return.
/// </summary>
@ -50,7 +55,43 @@ namespace SystemTrayMenu.Helper
/// Returns an icon for a given file - indicated by the name parameter.
/// </summary>
/// <returns>System.Drawing.Icon</returns>
public static Icon GetFileIcon(string filePath, bool linkOverlay,
public static Icon GetFileIconWithCache(string filePath, bool linkOverlay,
IconSize size = IconSize.Small)
{
Icon icon = null;
string extension = Path.GetExtension(filePath);
bool IsExtensionWitSameIcon(string fileExtension)
{
bool isExtensionWitSameIcon = true;
List<string> extensionsWithDiffIcons = new List<string>
{ ".exe", ".ink", ".ico", ".url" };
if (extensionsWithDiffIcons.Contains(fileExtension.ToLower()))
{
isExtensionWitSameIcon = false;
}
return isExtensionWitSameIcon;
}
if (IsExtensionWitSameIcon(extension))
{
if (dictIconCache.ContainsKey(extension))
{
icon = dictIconCache[extension];
}
else
{
icon = GetFileIcon(filePath, linkOverlay, size);
dictIconCache.Add(extension, icon);
}
}
else
{
icon = GetFileIcon(filePath, linkOverlay, size);
}
return icon;
}
private static Icon GetFileIcon(string filePath, bool linkOverlay,
IconSize size = IconSize.Small)
{
Icon icon = null;
@ -81,9 +122,14 @@ namespace SystemTrayMenu.Helper
{
IntPtr hIcon;
if (linkOverlay)
{
hIcon = shfi.hIcon; // Get icon directly
}
else
hIcon = Shell32.ImageList_GetIcon(hImageList, shfi.iIcon, Shell32.ILD_TRANSPARENT); // Get icon from .ink without overlay
{
// Get icon from .ink without overlay
hIcon = Shell32.ImageList_GetIcon(hImageList, shfi.iIcon, Shell32.ILD_TRANSPARENT);
}
try
{
@ -101,6 +147,7 @@ namespace SystemTrayMenu.Helper
if (!linkOverlay) User32.DestroyIcon(hIcon);
User32.DestroyIcon(shfi.hIcon);
}
return icon;
}

View file

@ -1,12 +1,12 @@
using Clearcove.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Threading;
@ -60,11 +60,9 @@ namespace SystemTrayMenu
if (!cancelAppRun)
{
Application.ThreadException += Application_ThreadException;
void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
void Application_ThreadException(object sender, ThreadExceptionEventArgs threadException)
{
log.Error($"{e.Exception.ToString()}");
Logger.ShutDown();
Application.Exit();
AskUserSendErrorAndRestartApp(log, threadException.Exception);
}
Application.Run();
}
@ -72,7 +70,7 @@ namespace SystemTrayMenu
}
catch (Exception ex)
{
log.Error($"{ex.ToString()}");
AskUserSendErrorAndRestartApp(log, ex);
}
finally
{
@ -80,6 +78,24 @@ namespace SystemTrayMenu
}
}
static void AskUserSendErrorAndRestartApp(Logger log, Exception exception)
{
log.Error($"{exception.ToString()}");
if (MessageBox.Show("A problem has been encountered and the application needs to restart. " +
"Reporting this error will help us make our product better. Press yes to open your standard email app.",
"SystemTrayMenu BugSplat", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Process.Start("mailto:" + "markus@hofknecht.eu" +
"?subject=SystemTrayMenu Bug reported" +
"&body=" + exception.ToString());
}
Logger.ShutDown();
Process.Start(Assembly.GetExecutingAssembly().
ManifestModule.FullyQualifiedName);
}
static bool KillOtherSystemTrayMenus()
{
bool killedAProcess = false;