Skip to content

Commit

Permalink
修复线程异常退出导致的Mutex失效Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Sep 26, 2020
1 parent 9796027 commit f1ada5f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
9 changes: 6 additions & 3 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
v1.1.5
- 修复线程异常退出导致的Mutex失效Exception

v1.1.4
- 只支持单实例
- 修复Clipboard线程不安全造成的异常
- 只支持单实例
- 修复Clipboard线程不安全造成的异常

v1.1.3
- add mutex when writing/reading remote files
Expand Down Expand Up @@ -35,4 +38,4 @@ v1.0.2
- Fix bugs

v1.0.1
- 发布
- 发布
1 change: 0 additions & 1 deletion SyncClipboard/Control/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace SyncClipboard.Control
public class MainController:System.Windows.Forms.Control
{
private string notifyText;
private static int WM_CLIPBOARDUPDATE = 0x031D;

private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu;
Expand Down
2 changes: 1 addition & 1 deletion SyncClipboard/HttpWebResponseUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 CookieCollection savedCookies = null;
//private static CookieCollection savedCookies = null;

private static void SaveCookies(CookieCollection cookies)
{
Expand Down
32 changes: 31 additions & 1 deletion SyncClipboard/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Profile(String jsonStr)
catch (ArgumentException)
{
Console.WriteLine("Existed profile file's format is wrong");
}
}
}

private static System.Threading.Mutex clipboardMutex = new System.Threading.Mutex();
Expand Down Expand Up @@ -71,6 +71,16 @@ public static Profile CreateFromLocalClipboard()

public static bool operator == (Profile lhs, Profile rhs)
{
if (System.Object.ReferenceEquals(lhs, rhs))
{
return true;
}

if (((object)lhs == null) || ((object)rhs == null))
{
return false;
}

if (lhs.Type != rhs.Type)
{
return false;
Expand All @@ -87,6 +97,26 @@ public static Profile CreateFromLocalClipboard()
{
return !(lhs == rhs);
}

public override bool Equals(Object obj)
{
return this == (Profile)obj;
}

public override int GetHashCode()
{
return this.ToString().GetHashCode();
}

public override string ToString()
{
string str = "";
str += "FileName" + FileName.ToString();
str += "Text:" + Text.ToString();
str += "Type:" + Type.ToString();
return str;
}


public Image GetImage()
{
Expand Down
11 changes: 7 additions & 4 deletions SyncClipboard/PullService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ private void PullLoop()

errorTimes = 0;
Profile remoteProfile = new Profile(strReply);
Profile localProfile = Profile.CreateFromLocalClipboard();
if (!clipboardChanged && remoteProfile != localProfile)
if (!clipboardChanged)
{
Clipboard.SetData(DataFormats.Text, remoteProfile.Text);
Notify(true, false, "剪切板同步成功", remoteProfile.Text, null, "info");
Profile localProfile = Profile.CreateFromLocalClipboard();
if (remoteProfile != localProfile)
{
Clipboard.SetData(DataFormats.Text, remoteProfile.Text);
Notify(true, false, "剪切板同步成功", remoteProfile.Text, null, "info");
}
}
Notify(false, true, "服务器连接成功", null, "正在同步", "info");
}
Expand Down
18 changes: 12 additions & 6 deletions SyncClipboard/PushService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PushService
private Notify Notify;
private bool switchOn = false;
private Thread pushThread = null;
private Profile currentProfile;

public PushService(Notify notifyFunction)
{
Expand Down Expand Up @@ -52,6 +53,7 @@ private void ClipboardChangedHandler()
pushThread = null;
}

currentProfile = Profile.CreateFromLocalClipboard();
pushThread = new Thread(UploadClipBoard);
pushThread.SetApartmentState(ApartmentState.STA);
pushThread.Start();
Expand All @@ -60,9 +62,8 @@ private void ClipboardChangedHandler()
private void UploadLoop()
{
Console.WriteLine("Push start " + DateTime.Now.ToString());
Profile profile = Profile.CreateFromLocalClipboard();

if (profile.Type == Profile.ClipboardType.None)
if (currentProfile.Type == Profile.ClipboardType.None)
{
return;
}
Expand All @@ -72,11 +73,11 @@ private void UploadLoop()
{
try
{
if (profile.Type == Profile.ClipboardType.Image)
if (currentProfile.Type == Profile.ClipboardType.Image)
{
HttpWebResponseUtility.PutImage(Config.GetImageUrl(), profile.GetImage(), Config.TimeOut, Config.GetHttpAuthHeader());
HttpWebResponseUtility.PutImage(Config.GetImageUrl(), currentProfile.GetImage(), Config.TimeOut, Config.GetHttpAuthHeader());
}
HttpWebResponseUtility.PutText(Config.GetProfileUrl(), profile.ToJsonString(), Config.TimeOut, Config.GetHttpAuthHeader());
HttpWebResponseUtility.PutText(Config.GetProfileUrl(), currentProfile.ToJsonString(), Config.TimeOut, Config.GetHttpAuthHeader());
Console.WriteLine("Push end " + DateTime.Now.ToString());
return;
}
Expand All @@ -86,11 +87,16 @@ private void UploadLoop()
}
Thread.Sleep(1000);
}
Notify(true, false, errMessage, "未同步:" + profile.Text, null, "erro");
Notify(true, false, errMessage, "未同步:" + currentProfile.Text, null, "erro");
}

private void UploadClipBoard()
{
if (currentProfile == null)
{
return;
}

RemoteClipboardLocker.Lock();
try
{
Expand Down
2 changes: 1 addition & 1 deletion SyncClipboard/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SyncClipboard
{
class UpdateChecker
{
public const string Version = "1.1.4";
public const string Version = "1.1.5";
public const string UpdateUrl = "https://api.github.com/repos/Jeric-X/SyncClipboard/releases/latest";
public const string ReleaseUrl = "https://github.com/Jeric-X/SyncClipboard/releases/latest";

Expand Down

0 comments on commit f1ada5f

Please sign in to comment.