Skip to content

Commit

Permalink
使用Task代替Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Apr 17, 2021
1 parent c52402f commit bb81585
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 50 deletions.
3 changes: 1 addition & 2 deletions SyncClipboard/Control/InputBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public static string Show(string Title, string defaultText = "")
InputBox inputbox = new InputBox();
inputbox.Text = Title;
inputbox.SetText(defaultText);
var a = inputbox.ShowDialog();
SyncClipboard.Utility.Log.Write(a.ToString());
inputbox.ShowDialog();
return inputbox._textBox.Text;
}
}
Expand Down
102 changes: 55 additions & 47 deletions SyncClipboard/Service/PushService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using SyncClipboard.Utility;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SyncClipboard
{
Expand All @@ -16,8 +17,12 @@ public class PushService
string _statusString = "";

private bool switchOn = false;
private Thread pushThread = null;

private CancellationTokenSource _cancelToken = new CancellationTokenSource();
private Task _task = Task.Run(() => {/* Do Nothing */});

private Profile currentProfile;
private Object _currentProfileMutex = new Object();

private int pushThreadNumber = 0;
public delegate void PushStatusChangingHandler();
Expand Down Expand Up @@ -94,79 +99,82 @@ public void Load()

private void ClipboardChangedHandler()
{
Mutex mutex = new Mutex();
mutex.WaitOne();
if (pushThread != null)
lock (_currentProfileMutex)
{
Log.Write("Kill old push thread");
pushThread.Abort();
pushThread = null;
currentProfile = ProfileFactory.CreateFromLocal();
if (currentProfile == null)
{
return;
}
if (currentProfile.GetProfileType() == ProfileType.ClipboardType.Unknown)
{
Log.Write("[PUSH] Local profile type is Unkown, stop upload.");
return;
}
}

currentProfile = ProfileFactory.CreateFromLocal();
if (currentProfile == null)
lock (_task)
{
return;
_cancelToken.Cancel();
_cancelToken = new CancellationTokenSource();

AddPushThreadNumber();

_task = _task.ContinueWith(
(Task task) =>
{
UploadLoop(_cancelToken.Token, _task);
},
_cancelToken.Token
).ContinueWith(
(Task task) =>
{
ReleasePushThreadNumber();
}
);
}
if (currentProfile.GetProfileType() == ProfileType.ClipboardType.Unknown)
{
Log.Write("[PUSH] Local profile type is Unkown, stop upload.");
return;
}

AddPushThreadNumber();
pushThread = new Thread(UploadClipBoard);
pushThread.SetApartmentState(ApartmentState.STA);
pushThread.Start();
Thread.Sleep(50);
Log.Write("Create new push thread");
mutex.ReleaseMutex();
}

private void UploadLoop(Profile currentProfile)
private void UploadLoop(CancellationToken token, Task iii)
{
Log.Write("Push start");

Log.Write("[PUSH] start loop");
RemoteClipboardLocker.Lock();
string errMessage = "";
for (int i = 0; i < UserConfig.Config.Program.RetryTimes && switchOn; i++)
{
if (token.IsCancellationRequested)
{
RemoteClipboardLocker.Unlock();
return;
}

try
{
currentProfile.UploadProfile();
Log.Write("Push end");
Log.Write("[PUSH] upload end");
RemoteClipboardLocker.Unlock();
return;
}
catch (Exception ex)
{
errMessage = ex.Message.ToString();
_notifyer.SetStatusString(SERVICE_NAME, $"失败,正在第{i + 1}次尝试,错误原因:{errMessage}", _isErrorStatus);
}
Thread.Sleep(1000);

if (token.IsCancellationRequested)
{
Log.Write("DDDD" + token.IsCancellationRequested);
RemoteClipboardLocker.Unlock();
return;
}
Thread.Sleep(UserConfig.Config.Program.IntervalTime);
}
RemoteClipboardLocker.Unlock();
_notifyer.ToastNotify("上传失败:" + currentProfile.ToolTip(), errMessage);
_statusString = errMessage;
_isErrorStatus = true;
}

private void UploadClipBoard()
{
RemoteClipboardLocker.Lock();
try
{
UploadLoop(currentProfile);
}
catch (Exception ex)
{
Log.Write(ex.Message.ToString());
}
finally
{
RemoteClipboardLocker.Unlock();
Log.Write("[PUSH] unlock remote");
ReleasePushThreadNumber();
}
}

private void SetUploadingIcon()
{
System.Drawing.Icon[] icon =
Expand Down
2 changes: 1 addition & 1 deletion SyncClipboard/Utility/HttpWebResponseUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SyncClipboard
{
public class HttpWebResponseUtility
{
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
private static readonly string DefaultUserAgent = "SyncClipboard";
//private static CookieCollection savedCookies = null;

private static void SaveCookies(CookieCollection cookies)
Expand Down

0 comments on commit bb81585

Please sign in to comment.