Skip to content

Commit

Permalink
对远程文件操作时加锁
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeric-X committed Sep 24, 2020
1 parent 68885fb commit 02813bc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions SyncClipboard/PullService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private void PullLoop()
int errorTimes = 0;
while (switchOn)
{
RemoteClipboardLocker.Lock();
String strReply = "";
clipboardChanged = false;
try
Expand All @@ -77,6 +78,7 @@ private void PullLoop()
continue;
}

RemoteClipboardLocker.Unlock();
errorTimes = 0;
Profile remoteProfile = new Profile(strReply);
Profile localProfile = Profile.CreateFromLocalClipboard();
Expand Down
19 changes: 18 additions & 1 deletion SyncClipboard/PushService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void ClipboardChangedHandler()
pushThread.Start();
}

private void UploadClipBoard(Object ClipboardData)
private void UploadLoop()
{
Console.WriteLine("Push start " + DateTime.Now.ToString());
Profile profile = Profile.CreateFromLocalClipboard();
Expand Down Expand Up @@ -88,5 +88,22 @@ private void UploadClipBoard(Object ClipboardData)
}
Notify(true, false, errMessage, "未同步:" + profile.Text, null, "erro");
}

private void UploadClipBoard()
{
RemoteClipboardLocker.Lock();
try
{
UploadLoop();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
RemoteClipboardLocker.Unlock();
}
}
}
}
19 changes: 19 additions & 0 deletions SyncClipboard/RemoteClipboardLocker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading;

namespace SyncClipboard
{
public static class RemoteClipboardLocker
{
static Mutex mutex = new Mutex();

public static void Lock()
{
mutex.WaitOne();
}

public static void Unlock()
{
mutex.ReleaseMutex();
}
}
}
1 change: 1 addition & 0 deletions SyncClipboard/SyncClipboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="RemoteClipboardLocker.cs" />
<Compile Include="ClipboardListener.cs">
<SubType>Component</SubType>
</Compile>
Expand Down

0 comments on commit 02813bc

Please sign in to comment.