diff --git a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs index c31ab65..773bb62 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs @@ -45,7 +45,7 @@ namespace QuickLook.Plugin.ArchiveViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { GC.SuppressFinalize(this); @@ -54,7 +54,7 @@ namespace QuickLook.Plugin.ArchiveViewer ~Plugin() { - Dispose(); + Cleanup(); } } } \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs index 76548f1..d9f892a 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs @@ -43,7 +43,7 @@ namespace QuickLook.Plugin.HtmlViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { GC.SuppressFinalize(this); @@ -52,7 +52,7 @@ namespace QuickLook.Plugin.HtmlViewer ~Plugin() { - Dispose(); + Cleanup(); } } } \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs index 05aeaa1..31f1511 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs @@ -50,7 +50,7 @@ namespace QuickLook.Plugin.ImageViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { } } diff --git a/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs index 97f1cd5..d9037a4 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs @@ -44,7 +44,7 @@ namespace QuickLook.Plugin.MarkdownViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { GC.SuppressFinalize(this); @@ -53,7 +53,7 @@ namespace QuickLook.Plugin.MarkdownViewer ~Plugin() { - Dispose(); + Cleanup(); } private string GenerateMarkdownHtml(string path) diff --git a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs b/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs index 9e848a3..4b30530 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.OfficeViewer/PluginInterface.cs @@ -69,7 +69,7 @@ namespace QuickLook.Plugin.OfficeViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { GC.SuppressFinalize(this); @@ -89,7 +89,7 @@ namespace QuickLook.Plugin.OfficeViewer ~PluginInterface() { - Dispose(); + Cleanup(); } } } \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs index 0533c28..ee3f990 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs @@ -49,7 +49,7 @@ namespace QuickLook.Plugin.PDFViewer }; } - public void Dispose() + public void Cleanup() { GC.SuppressFinalize(this); @@ -59,7 +59,7 @@ namespace QuickLook.Plugin.PDFViewer ~Plugin() { - Dispose(); + Cleanup(); } } } \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs index 1040a3e..51aae27 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs @@ -56,7 +56,7 @@ namespace QuickLook.Plugin.TextViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { } } diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs index ec1076e..0c614af 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/PluginInterface.cs @@ -52,7 +52,7 @@ namespace QuickLook.Plugin.VideoViewer context.IsBusy = false; } - public void Dispose() + public void Cleanup() { _vp?.Dispose(); } diff --git a/QuickLook/MainWindow.xaml.cs b/QuickLook/MainWindow.xaml.cs index 866c78c..b8afdfb 100644 --- a/QuickLook/MainWindow.xaml.cs +++ b/QuickLook/MainWindow.xaml.cs @@ -77,13 +77,16 @@ namespace QuickLook private new void Hide() { + if (App.RunningAsViewer) + Application.Current.Shutdown(); + container.Content = null; // clean up plugin and refresh ContextObject for next use - ContextObject.ViewerPlugin?.Dispose(); + ContextObject.ViewerPlugin?.Cleanup(); ContextObject.Reset(); - //GC.Collect(); + GC.Collect(); // revert UI changes ContextObject.IsBusy = true; diff --git a/QuickLook/PidHelper.cs b/QuickLook/PidHelper.cs index 059d447..fcdb922 100644 --- a/QuickLook/PidHelper.cs +++ b/QuickLook/PidHelper.cs @@ -5,6 +5,8 @@ namespace QuickLook { internal static class PidHelper { + private static FileStream _pidLocker; + private static readonly string PidListener = Path.Combine(Path.GetTempPath(), "QuickLook.App.Listener.D6EC3F8DDF6B.pid"); @@ -35,11 +37,18 @@ namespace QuickLook internal static void WritePid() { - File.WriteAllText(App.RunningAsViewer ? PidViewer : PidListener, Process.GetCurrentProcess().Id.ToString()); + var pidFile = App.RunningAsViewer ? PidViewer : PidListener; + + File.WriteAllText(pidFile, Process.GetCurrentProcess().Id.ToString()); + + _pidLocker = File.Open(pidFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read); } internal static void DeletePid() { + _pidLocker?.Close(); + _pidLocker = null; + File.Delete(App.RunningAsViewer ? PidViewer : PidListener); } } diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index 1635d91..9bb502d 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -70,7 +70,7 @@ namespace QuickLook.Plugin public void DisposePlugin() { - ViewerPlugin?.Dispose(); + ViewerPlugin?.Cleanup(); ViewerPlugin = null; } diff --git a/QuickLook/Plugin/IViewer.cs b/QuickLook/Plugin/IViewer.cs index fb4a8bb..cf2eb5f 100644 --- a/QuickLook/Plugin/IViewer.cs +++ b/QuickLook/Plugin/IViewer.cs @@ -35,6 +35,6 @@ /// /// Release any unmanaged resource here. /// - void Dispose(); + void Cleanup(); } } \ No newline at end of file diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml index 8d3c7ab..136766e 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml @@ -11,7 +11,24 @@ - + + + + + diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs index 18a44fd..8cb5d06 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System; +using System.Globalization; using System.IO; using System.Threading.Tasks; using System.Windows.Controls; @@ -25,15 +26,19 @@ namespace QuickLook.Plugin.InfoPanel public void DisplayInfo(string path) { - var icon = - WindowsThumbnailProvider.GetThumbnail(path, - (int) (128 * DpiHelper.GetCurrentDpi().HorizontalDpi / DpiHelper.DEFAULT_DPI), - (int) (128 * DpiHelper.GetCurrentDpi().VerticalDpi / DpiHelper.DEFAULT_DPI), - ThumbnailOptions.ScaleUp); + Task.Run(() => + { + var icon = + WindowsThumbnailProvider.GetThumbnail(path, + (int) (128 * DpiHelper.GetCurrentDpi().HorizontalDpi / DpiHelper.DEFAULT_DPI), + (int) (128 * DpiHelper.GetCurrentDpi().VerticalDpi / DpiHelper.DEFAULT_DPI), + ThumbnailOptions.ScaleUp); - image.Source = icon.ToBitmapSource(); + var source = icon.ToBitmapSource(); + icon.Dispose(); - icon.Dispose(); + Dispatcher.BeginInvoke(new Action(() => image.Source = source)); + }); var name = Path.GetFileName(path); filename.Text = string.IsNullOrEmpty(name) ? path : name; diff --git a/QuickLook/Plugin/InfoPanel/PluginInterface.cs b/QuickLook/Plugin/InfoPanel/PluginInterface.cs index c497463..d1cf4f0 100644 --- a/QuickLook/Plugin/InfoPanel/PluginInterface.cs +++ b/QuickLook/Plugin/InfoPanel/PluginInterface.cs @@ -3,7 +3,7 @@ using System.Windows; namespace QuickLook.Plugin.InfoPanel { - public class PluginInterface : IViewer, IDisposable + public class PluginInterface : IViewer { private InfoPanel _ip; @@ -31,14 +31,9 @@ namespace QuickLook.Plugin.InfoPanel context.IsBusy = false; } - public void Dispose() + public void Cleanup() { _ip.Stop = true; } - - ~PluginInterface() - { - Dispose(); - } } } \ No newline at end of file diff --git a/QuickLook/PluginManager.cs b/QuickLook/PluginManager.cs index ab4896e..adbae85 100644 --- a/QuickLook/PluginManager.cs +++ b/QuickLook/PluginManager.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Reflection; using QuickLook.ExtensionMethods; using QuickLook.Plugin; -using QuickLook.Plugin.InfoPanel; namespace QuickLook { @@ -18,9 +17,9 @@ namespace QuickLook LoadPlugins(); } - internal Type DefaultPlugin { get; } = typeof(PluginInterface); + internal IViewer DefaultPlugin { get; } = new Plugin.InfoPanel.PluginInterface(); - internal List LoadedPlugins { get; private set; } = new List(); + internal List LoadedPlugins { get; private set; } = new List(); internal static PluginManager GetInstance() { @@ -38,17 +37,16 @@ namespace QuickLook var can = false; try { - can = plugin.CreateInstance().CanHandle(path); + can = plugin.CanHandle(path); } catch (Exception) { // ignored } return can; - }) - ?.CreateInstance(); + }); - return matched ?? DefaultPlugin.CreateInstance(); + return matched ?? DefaultPlugin; } private void LoadPlugins() @@ -63,10 +61,10 @@ namespace QuickLook where !t.IsInterface && !t.IsAbstract where typeof(IViewer).IsAssignableFrom(t) select t).ToList() - .ForEach(type => LoadedPlugins.Add(type)); + .ForEach(type => LoadedPlugins.Add(type.CreateInstance())); }); - LoadedPlugins = LoadedPlugins.OrderByDescending(i => i.CreateInstance().Priority).ToList(); + LoadedPlugins = LoadedPlugins.OrderByDescending(i => i.Priority).ToList(); } } } \ No newline at end of file diff --git a/QuickLook/ViewWindowManager.cs b/QuickLook/ViewWindowManager.cs index 8832183..0960780 100644 --- a/QuickLook/ViewWindowManager.cs +++ b/QuickLook/ViewWindowManager.cs @@ -20,11 +20,6 @@ namespace QuickLook internal ViewWindowManager() { _viewWindow = new MainWindow(); - _viewWindow.Closed += (sender, e) => - { - if (App.RunningAsViewer) - Application.Current.Shutdown(); - }; } internal void InvokeRoutine() @@ -66,10 +61,10 @@ namespace QuickLook Debug.WriteLine(e.ToString()); Debug.WriteLine(e.StackTrace); - if (matchedPlugin.GetType() != PluginManager.GetInstance().DefaultPlugin) + if (matchedPlugin != PluginManager.GetInstance().DefaultPlugin) { - matchedPlugin.Dispose(); - matchedPlugin = PluginManager.GetInstance().DefaultPlugin.CreateInstance(); + matchedPlugin.Cleanup(); + matchedPlugin = PluginManager.GetInstance().DefaultPlugin; BeginShowNewWindow(matchedPlugin, path); } else