Avoid race condition on changes. Ensure timers restart even on exceptions.

This commit is contained in:
Simon Pither 2010-07-03 20:37:43 +01:00
parent f73aff54bb
commit 6e76fadd36

View file

@ -35,6 +35,7 @@ namespace SparkleShare {
private FileSystemWatcher Watcher;
private bool HasChanged = false;
private DateTime LastChange;
private System.Object ChangeLock = new System.Object();
public string Name;
public string Domain;
@ -126,6 +127,7 @@ namespace SparkleShare {
BufferTimer.Elapsed += delegate (object o, ElapsedEventArgs args) {
SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Checking for changes.");
lock(ChangeLock) {
if (HasChanged) {
SparkleHelpers.DebugInfo ("Buffer", "[" + Name + "] Changes found, checking if settled.");
DateTime now = DateTime.UtcNow;
@ -136,6 +138,7 @@ namespace SparkleShare {
AddCommitAndPush ();
}
}
}
};
BufferTimer.Start ();
@ -156,15 +159,18 @@ namespace SparkleShare {
if (!ShouldIgnore (args.Name)) {
SparkleHelpers.DebugInfo ("Event", "[" + Name + "] " + wct.ToString () + " '" + args.Name + "'");
FetchTimer.Stop ();
lock(ChangeLock) {
LastChange = DateTime.UtcNow;
HasChanged = true;
}
}
}
// When there are changes we generally want to Add, Commit and Push
// so this method does them all with appropriate timers, etc switched off
public void AddCommitAndPush ()
{
try {
BufferTimer.Stop ();
FetchTimer.Stop ();
@ -174,13 +180,13 @@ namespace SparkleShare {
Commit (Message);
Fetch ();
Push ();
SparkleHelpers.CheckForUnicorns (Message);
}
}
finally {
FetchTimer.Start ();
BufferTimer.Start ();
SparkleHelpers.CheckForUnicorns (Message);
}
}
// Stages the made changes
@ -207,6 +213,7 @@ namespace SparkleShare {
// Fetches changes from the remote repo
public void Fetch ()
{
try {
FetchTimer.Stop ();
// SparkleUI.NotificationIcon.SetSyncingState ();
SparkleHelpers.DebugInfo ("Git", "[" + Name + "] Fetching changes...");
@ -218,8 +225,11 @@ namespace SparkleShare {
if (!Output.Contains ("up to date"))
Rebase ();
// SparkleUI.NotificationIcon.SetIdleState ();
}
finally {
FetchTimer.Start ();
}
}
// Merges the fetched changes
public void Rebase ()