fix: stabilize PPT slideshow state sync on transient COM states (#394)
This commit is contained in:
@@ -67,6 +67,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
private readonly object _lockObject = new object();
|
private readonly object _lockObject = new object();
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
private static bool IsPptBusyHResult(uint hr) => hr == 0x80010001 || hr == 0x8001010A;
|
private static bool IsPptBusyHResult(uint hr) => hr == 0x80010001 || hr == 0x8001010A;
|
||||||
|
private static bool IsNoActivePresentationOrWindowHResult(uint hr) => hr == 0x80048240;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructor & Initialization
|
#region Constructor & Initialization
|
||||||
@@ -537,7 +538,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateCurrentPresentationInfo()
|
private void UpdateCurrentPresentationInfo(bool preferSlideShowWindow = false, SlideShowWindow knownSlideShowWindow = null)
|
||||||
{
|
{
|
||||||
object activePresentation = null;
|
object activePresentation = null;
|
||||||
object slideShowWindows = null;
|
object slideShowWindows = null;
|
||||||
@@ -553,7 +554,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
activePresentation = PPTApplication.ActivePresentation;
|
activePresentation = TryGetActivePresentation(knownSlideShowWindow);
|
||||||
if (activePresentation != null)
|
if (activePresentation != null)
|
||||||
{
|
{
|
||||||
SafeReleaseComObject(CurrentPresentation, "CurrentPresentation");
|
SafeReleaseComObject(CurrentPresentation, "CurrentPresentation");
|
||||||
@@ -583,21 +584,30 @@ namespace Ink_Canvas.Helpers
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
slideShowWindows = PPTApplication.SlideShowWindows;
|
slideShowWindows = PPTApplication.SlideShowWindows;
|
||||||
if (IsInSlideShow && slideShowWindows != null)
|
var shouldUseSlideShowWindow = preferSlideShowWindow || IsInSlideShow;
|
||||||
|
if (shouldUseSlideShowWindow && slideShowWindows != null)
|
||||||
{
|
{
|
||||||
dynamic ssw = slideShowWindows;
|
if (knownSlideShowWindow != null)
|
||||||
if (ssw.Count > 0)
|
|
||||||
{
|
{
|
||||||
slideShowWindow = ssw[1];
|
slideShowWindow = knownSlideShowWindow;
|
||||||
if (slideShowWindow != null)
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dynamic ssw = slideShowWindows;
|
||||||
|
if (ssw.Count > 0)
|
||||||
{
|
{
|
||||||
dynamic sswObj = slideShowWindow;
|
slideShowWindow = ssw[1];
|
||||||
view = sswObj.View;
|
}
|
||||||
if (view != null)
|
}
|
||||||
{
|
|
||||||
dynamic viewObj = view;
|
if (slideShowWindow != null)
|
||||||
|
{
|
||||||
|
dynamic sswObj = slideShowWindow;
|
||||||
|
view = sswObj.View;
|
||||||
|
if (view != null)
|
||||||
|
{
|
||||||
|
dynamic viewObj = view;
|
||||||
CurrentSlide = viewObj.Slide as Slide;
|
CurrentSlide = viewObj.Slide as Slide;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -633,7 +643,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
catch (COMException comEx)
|
catch (COMException comEx)
|
||||||
{
|
{
|
||||||
var hr = (uint)comEx.HResult;
|
var hr = (uint)comEx.HResult;
|
||||||
if (hr != 0x8001010E && hr != 0x80004005)
|
if (hr != 0x8001010E && hr != 0x80004005 && !IsNoActivePresentationOrWindowHResult(hr))
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"获取当前幻灯片失败: {comEx.Message}", LogHelper.LogType.Warning);
|
LogHelper.WriteLogToFile($"获取当前幻灯片失败: {comEx.Message}", LogHelper.LogType.Warning);
|
||||||
}
|
}
|
||||||
@@ -655,7 +665,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
catch (COMException comEx)
|
catch (COMException comEx)
|
||||||
{
|
{
|
||||||
var hr = (uint)comEx.HResult;
|
var hr = (uint)comEx.HResult;
|
||||||
if (hr == 0x8001010E || hr == 0x80004005)
|
if (hr == 0x8001010E || hr == 0x80004005 || IsNoActivePresentationOrWindowHResult(hr))
|
||||||
{
|
{
|
||||||
CurrentPresentation = null;
|
CurrentPresentation = null;
|
||||||
CurrentSlides = null;
|
CurrentSlides = null;
|
||||||
@@ -698,6 +708,61 @@ namespace Ink_Canvas.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private object TryGetActivePresentation(SlideShowWindow knownSlideShowWindow)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return PPTApplication.ActivePresentation;
|
||||||
|
}
|
||||||
|
catch (COMException comEx)
|
||||||
|
{
|
||||||
|
var hr = (uint)comEx.HResult;
|
||||||
|
if (!IsNoActivePresentationOrWindowHResult(hr))
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (knownSlideShowWindow != null)
|
||||||
|
{
|
||||||
|
return knownSlideShowWindow.Presentation;
|
||||||
|
}
|
||||||
|
|
||||||
|
var slideShowWindows = PPTApplication.SlideShowWindows;
|
||||||
|
if (slideShowWindows == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic ssw = slideShowWindows;
|
||||||
|
if (ssw.Count == 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var slideShowWindow = ssw[1];
|
||||||
|
if (slideShowWindow == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic sswObj = slideShowWindow;
|
||||||
|
return sswObj.Presentation;
|
||||||
|
}
|
||||||
|
catch (COMException comEx)
|
||||||
|
{
|
||||||
|
var hr = (uint)comEx.HResult;
|
||||||
|
if (!IsNoActivePresentationOrWindowHResult(hr))
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Event Handlers
|
#region Event Handlers
|
||||||
@@ -734,7 +799,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
_cachedIsInSlideShow = true;
|
_cachedIsInSlideShow = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateCurrentPresentationInfo();
|
UpdateCurrentPresentationInfo(preferSlideShowWindow: true, knownSlideShowWindow: wn);
|
||||||
SlideShowBegin?.Invoke(wn);
|
SlideShowBegin?.Invoke(wn);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -747,7 +812,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateCurrentPresentationInfo();
|
UpdateCurrentPresentationInfo(preferSlideShowWindow: true, knownSlideShowWindow: wn);
|
||||||
SlideShowNextSlide?.Invoke(wn);
|
SlideShowNextSlide?.Invoke(wn);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user