private async Task SetRemoteProfileToLocal(Profile remoteProfile) { Profile localProfile = ProfileFactory.CreateFromLocal(); if (localProfile.GetProfileType() == ProfileType.ClipboardType.Unknown) { Log.Write("[PULL] Local profile type is Unkown, stop sync."); return; } Log.Write("[PULL] isChangingRemote = " + _isChangingRemote.ToString()); if (!_isChangingRemote && remoteProfile != localProfile) { Thread.Sleep(200); if (!_isChangingRemote) { SetDownloadingIcon(); PullStarted?.Invoke(); await remoteProfile.SetLocalClipboard().ConfigureAwait(true); Log.Write("剪切板同步成功:" + remoteProfile.Text); Global.Notifyer.ToastNotify("剪切板同步成功", remoteProfile.ToolTip(), remoteProfile.ExecuteProfile()); StopDownloadingIcon(); Thread.Sleep(50); PullStopped?.Invoke(); } } }
private async Task UploadClipboard() { var currentProfile = ProfileFactory.CreateFromLocal(); if (currentProfile == null) { Log.Write("Local profile type is null, stop upload."); return; } if (currentProfile == null || currentProfile.GetProfileType() == ProfileType.ClipboardType.Unknown) { Log.Write("Local profile type is Unkown, stop upload."); return; } await UploadLoop(currentProfile).ConfigureAwait(true); }
private async Task PullLoop() { int errorTimes = 0; while (!_cancelToken.IsCancellationRequested) { Global.Notifyer.SetStatusString(SERVICE_NAME, "Reading remote profile."); SyncService.remoteProfilemutex.WaitOne(); Profile remoteProfile = null; try { remoteProfile = await ProfileFactory.CreateFromRemote(Global.WebDav).ConfigureAwait(true); await SetRemoteProfileToLocal(remoteProfile).ConfigureAwait(true); Global.Notifyer.SetStatusString(SERVICE_NAME, "Running.", false); errorTimes = 0; } catch (Exception ex) { errorTimes++; Global.Notifyer.SetStatusString(SERVICE_NAME, $"Error. Failed times: {errorTimes}.", true); Log.Write(ex.ToString()); if (errorTimes == UserConfig.Config.Program.RetryTimes) { Global.Notifyer.ToastNotify("剪切板同步失败", ex.Message); } } finally { SyncService.remoteProfilemutex.ReleaseMutex(); } await Task.Delay(UserConfig.Config.Program.IntervalTime).ConfigureAwait(true); } }