Skip to content

Commit 90e52eb

Browse files
committed
retry when flushing clipboard failed
1 parent 2a5a5b0 commit 90e52eb

File tree

1 file changed

+37
-13
lines changed
  • src/Flow.Launcher.Plugin.ClipboardPlus

1 file changed

+37
-13
lines changed

src/Flow.Launcher.Plugin.ClipboardPlus/Main.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public class ClipboardPlus : IAsyncPlugin, IAsyncReloadable, IContextMenu, IPlug
5050
// Clipboard retry times
5151
private const int ClipboardRetryTimes = 5;
5252

53-
// Clipboard retry interval
54-
private const int ClipboardRetryInterval = 100;
53+
// Retry interval
54+
private const int RetryInterval = 100;
5555

5656
#region Scores
5757

@@ -646,7 +646,7 @@ private async Task WaitWindowHideAndSimulatePaste()
646646
{
647647
while (Context.API.IsMainWindowVisible())
648648
{
649-
await Task.Delay(100);
649+
await Task.Delay(RetryInterval);
650650
}
651651
new InputSimulator().Keyboard.ModifiedKeyStroke(
652652
VirtualKeyCode.CONTROL,
@@ -747,7 +747,7 @@ private async void CopyToClipboard(ClipboardData clipboardData)
747747
{
748748
return e;
749749
}
750-
await Task.Delay(ClipboardRetryInterval);
750+
await Task.Delay(RetryInterval);
751751
}
752752
}
753753
return null;
@@ -807,7 +807,7 @@ private async void CopyAsPlainTextToClipboard(ClipboardData clipboardData)
807807
{
808808
return e;
809809
}
810-
await Task.Delay(ClipboardRetryInterval);
810+
await Task.Delay(RetryInterval);
811811
}
812812
}
813813
return null;
@@ -848,10 +848,31 @@ private async void ReQuery()
848848
{
849849
// TODO: Ask Flow-Launcher for a better way to exit the context menu.
850850
new InputSimulator().Keyboard.KeyPress(VirtualKeyCode.ESCAPE);
851-
await Task.Delay(100);
851+
await Task.Delay(RetryInterval);
852852
Context.API.ReQuery(false);
853853
}
854854

855+
private static async Task<Exception?> FlushClipboard()
856+
{
857+
for (int i = 0; i < ClipboardRetryTimes; i++)
858+
{
859+
try
860+
{
861+
Clipboard.Flush();
862+
break;
863+
}
864+
catch (Exception e)
865+
{
866+
if (i == ClipboardRetryTimes - 1)
867+
{
868+
return e;
869+
}
870+
await Task.Delay(RetryInterval);
871+
}
872+
}
873+
return null;
874+
}
875+
855876
#endregion
856877

857878
#region IClipboardPlus Interface
@@ -890,7 +911,7 @@ public void Dispose()
890911
GC.SuppressFinalize(this);
891912
}
892913

893-
protected virtual void Dispose(bool disposing)
914+
protected async void Dispose(bool disposing)
894915
{
895916
if (disposing)
896917
{
@@ -899,21 +920,24 @@ protected virtual void Dispose(bool disposing)
899920
{
900921
DatabaseHelper?.Dispose();
901922
DatabaseHelper = null!;
902-
Context.API.LogInfo(ClassName, $"Disposed DatabaseHelper");
923+
Context.API.LogDebug(ClassName, $"Disposed DatabaseHelper");
903924
}
904925
ClipboardMonitor.ClipboardChanged -= OnClipboardChangeW;
905926
ClipboardMonitor.Dispose();
906927
ClipboardMonitor = null!;
907-
Context.API.LogInfo(ClassName, $"Disposed ClipboardMonitor");
928+
Context.API.LogDebug(ClassName, $"Disposed ClipboardMonitor");
908929
CultureInfoChanged = null;
909930
Settings = null!;
910931
RecordsList = null!;
911-
try
932+
var exception = await FlushClipboard();
933+
if (exception == null)
912934
{
913-
Clipboard.Flush();
914-
Context.API.LogInfo(ClassName, $"Flushed Clipboard");
935+
Context.API.LogDebug(ClassName, $"Flushed Clipboard");
936+
}
937+
else
938+
{
939+
Context.API.LogException(ClassName, $"Flushed Clipboard failed", exception);
915940
}
916-
catch (Exception) { }
917941
Context.API.LogWarn(ClassName, $"Finish dispose");
918942
_disposed = true;
919943
}

0 commit comments

Comments
 (0)