Skip to content

Commit

Permalink
修复:触发上传新剪贴板无法打断正在进行的下载远程剪贴板
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Jun 16, 2024
1 parent dfc529f commit af5363e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SyncClipboard.Abstract;
using SyncClipboard.Core.Interfaces;
using SyncClipboard.Core.Models;
using SyncClipboard.Core.Utilities;

namespace SyncClipboard.Core.Clipboard;

Expand Down Expand Up @@ -72,11 +73,11 @@ private void ReleaseRef()
{
try
{
ClipboardChangedImpl?.Invoke();
ClipboardChangedImpl?.GetInvocationList()?.ForEach(delegt => delegt.InvokeNoExcept());
var token = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token;
meta ??= await ClipboardFactory.GetMetaInfomation(token);
var profile = await ClipboardFactory.CreateProfileFromMeta(meta, token);
ChangedImpl?.Invoke(meta, profile);
ChangedImpl?.GetInvocationList()?.ForEach(delegt => delegt.InvokeNoExcept(meta, profile));
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,11 @@ private void SetWorkingStartStatus()
{
_trayIcon.ShowUploadAnimation();
_trayIcon.SetStatusString(SERVICE_NAME_SIMPLE, "Uploading.");
PushStarted?.Invoke();
}

private void SetWorkingEndStatus()
{
_trayIcon.StopAnimation();
PushStopped?.Invoke();
}

private bool IsDownloadServiceWorking(Profile profile)
Expand All @@ -191,6 +189,9 @@ private async Task<bool> IsObsoleteMeta(ClipboardMetaInfomation meta, Cancellati

protected override async Task HandleClipboard(ClipboardMetaInfomation meta, Profile profile, CancellationToken token)
{
PushStarted?.Invoke();
using var guard = new ScopeGuard(() => PushStopped?.Invoke());

await SyncService.remoteProfilemutex.WaitAsync(token);
try
{
Expand All @@ -201,6 +202,7 @@ protected override async Task HandleClipboard(ClipboardMetaInfomation meta, Prof
}

SetWorkingStartStatus();
using var workingStatusGuard = new ScopeGuard(SetWorkingEndStatus);
await UploadClipboard(profile, token);
}
catch (OperationCanceledException)
Expand All @@ -211,7 +213,6 @@ protected override async Task HandleClipboard(ClipboardMetaInfomation meta, Prof
{
SyncService.remoteProfilemutex.Release();
}
SetWorkingEndStatus();
}

private async Task UploadClipboard(Profile currentProfile, CancellationToken cancelToken)
Expand Down
12 changes: 12 additions & 0 deletions src/SyncClipboard.Core/Utilities/DelegateExtention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ public static void InvokeNoExcept(this Action action, string? logTag = null)
AppCore.Current?.Logger.Write(logTag, $"Invoke Unhandled Exception {ex.Message}\n{ex.StackTrace}");
}
}

public static void InvokeNoExcept(this Delegate dele, params object?[]? args)
{
try
{
dele.DynamicInvoke(args);
}
catch (Exception ex)
{
AppCore.Current?.Logger.Write($"Invoke Unhandled Exception {ex.Message}\n{ex.StackTrace}");
}
}
}
15 changes: 15 additions & 0 deletions src/SyncClipboard.Core/Utilities/ScopeGuard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SyncClipboard.Core.Utilities;

public sealed class ScopeGuard : IDisposable
{
private readonly Action _action;
public ScopeGuard(Action action)
{
_action = action;
}

public void Dispose()
{
_action?.Invoke();
}
}
2 changes: 1 addition & 1 deletion src/SyncClipboard.Desktop/Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- 修复:不设置hash上传图片/文件到服务器后,桌面客户端会无限重复设置剪贴板
- 修复:使用某些截图工具截图复制后,无法触发上传(https://github.com/Jeric-X/SyncClipboard/issues/88)
- 修复:远程文件不存在时,只报错一次,不再图标错乱(https://github.com/Jeric-X/SyncClipboard/issues/87)
- 为修复https://github.com/Jeric-X/SyncClipboard/issues/86,Linux无法自动识别语言,默认语言为英语
- 为修复 https://github.com/Jeric-X/SyncClipboard/issues/86,Linux无法自动识别语言,默认语言为英语
- 功能:手动上传后可以发送通知(https://github.com/Jeric-X/SyncClipboard/issues/82)

0.7.3
Expand Down

0 comments on commit af5363e

Please sign in to comment.