Properly use try/finally for releasing locks in ValidatingModelBase

This commit is contained in:
Antony Male 2016-08-26 11:37:33 +01:00
parent 7da6f8a363
commit 4532a9859d
1 changed files with 15 additions and 3 deletions

View File

@ -110,6 +110,7 @@ namespace Stylet
var changedProperties = new List<string>(); var changedProperties = new List<string>();
await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false); await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false);
try
{ {
foreach (var kvp in results) foreach (var kvp in results)
{ {
@ -130,7 +131,10 @@ namespace Stylet
changedProperties.Add(removedKey); changedProperties.Add(removedKey);
} }
} }
this.propertyErrorsLock.Release(); finally
{
this.propertyErrorsLock.Release();
}
if (changedProperties.Count > 0) if (changedProperties.Count > 0)
this.OnValidationStateChanged(changedProperties); this.OnValidationStateChanged(changedProperties);
@ -199,6 +203,7 @@ namespace Stylet
bool propertyErrorsChanged = false; bool propertyErrorsChanged = false;
await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false); await this.propertyErrorsLock.WaitAsync().ConfigureAwait(false);
try
{ {
if (!this.propertyErrors.ContainsKey(propertyName)) if (!this.propertyErrors.ContainsKey(propertyName))
this.propertyErrors.Add(propertyName, null); this.propertyErrors.Add(propertyName, null);
@ -209,7 +214,10 @@ namespace Stylet
propertyErrorsChanged = true; propertyErrorsChanged = true;
} }
} }
this.propertyErrorsLock.Release(); finally
{
this.propertyErrorsLock.Release();
}
if (propertyErrorsChanged) if (propertyErrorsChanged)
this.OnValidationStateChanged(new[] { propertyName }); this.OnValidationStateChanged(new[] { propertyName });
@ -271,10 +279,14 @@ namespace Stylet
// We'll just have to wait synchronously for this. Oh well. The lock shouldn't be long. // We'll just have to wait synchronously for this. Oh well. The lock shouldn't be long.
// Everything that awaits uses ConfigureAwait(false), so we shouldn't deadlock if someone calls this on the main thread // Everything that awaits uses ConfigureAwait(false), so we shouldn't deadlock if someone calls this on the main thread
this.propertyErrorsLock.Wait(); this.propertyErrorsLock.Wait();
try
{ {
this.propertyErrors.TryGetValue(propertyName, out errors); this.propertyErrors.TryGetValue(propertyName, out errors);
} }
this.propertyErrorsLock.Release(); finally
{
this.propertyErrorsLock.Release();
}
return errors; return errors;
} }