Skip to content

Commit 6f760c7

Browse files
author
Oren (electricessence)
committed
Reformat and contract improvments.
1 parent 1a2f2f1 commit 6f760c7

File tree

7 files changed

+390
-347
lines changed

7 files changed

+390
-347
lines changed

AsyncProcess.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
2-
using System.Threading.Tasks;
1+
using Open.Disposable;
2+
using System;
3+
using System.Diagnostics.Contracts;
34
using System.Threading;
4-
using Open.Disposable;
5+
using System.Threading.Tasks;
56

67
namespace Open.Threading
78
{
@@ -68,8 +69,9 @@ public bool HasBeenRun
6869
//long _processCount = 0;
6970
protected virtual void Process(object progress)
7071
{
71-
if(progress==null)
72-
throw new ArgumentNullException("progress");
72+
if (progress == null)
73+
throw new ArgumentNullException(nameof(progress));
74+
Contract.EndContractBlock();
7375

7476
var p = (T)progress;
7577
try
@@ -78,7 +80,7 @@ protected virtual void Process(object progress)
7880
Closure(p);
7981
SyncLock.Write(() => LatestCompleted = DateTime.Now);
8082
}
81-
catch(Exception ex)
83+
catch (Exception ex)
8284
{
8385
SyncLock.Write(() => LastFault = ex);
8486
}
@@ -92,18 +94,20 @@ protected virtual Task EnsureProcess(bool once, TimeSpan? timeAllowedBeforeRefre
9294
{
9395
Task task = null;
9496
SyncLock.ReadWriteConditionalOptimized(
95-
write=>{
97+
write =>
98+
{
9699
task = InternalTask;
97100
return (task == null || !once && !task.IsActive()) // No action, or completed?
98101
&& (!timeAllowedBeforeRefresh.HasValue // Now?
99102
|| timeAllowedBeforeRefresh.Value < DateTime.Now - LatestCompleted); // Or later?
100-
}, () => {
103+
}, () =>
104+
{
101105

102106
task = new Task(Process, new T());
103107
task.Start(Scheduler);
104108
InternalTask = task;
105109
Count++;
106-
110+
107111
}
108112
);
109113

@@ -126,7 +130,7 @@ public bool IsRunning
126130
public void Wait(bool once = true)
127131
{
128132
EnsureActive(once);
129-
InternalTask?.Wait();
133+
InternalTask?.Wait();
130134
}
131135

132136
public bool EnsureActive(bool once = false)

AsyncQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Open.Threading.Tasks
55
{
6-
public class AsyncQuery<TResult> : AsyncProcess
6+
public class AsyncQuery<TResult> : AsyncProcess
77
{
88
TResult _latest;
99

Container.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ protected virtual void OnValueUpdated(T originalValue, T newValue) { }
165165
private void OnValueInitializedInternal(T initValue)
166166
{
167167
OnValueInitialized(initValue);
168-
ValueInitialzed?.Invoke(this, initValue);
169-
}
168+
ValueInitialzed?.Invoke(this, initValue);
169+
}
170170

171171
private void OnValueUpdatedInternal(T originalValue, T newValue)
172172
{
173173
OnValueUpdated(originalValue, newValue);
174-
ValueUpdated?.Invoke(this, originalValue, newValue);
175-
}
174+
ValueUpdated?.Invoke(this, originalValue, newValue);
175+
}
176176
#endregion
177177

178178

ModificationSynchronizedBase.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,18 @@ bool SetSync(IModificationSynchronizer value, bool resetOwnership = true)
6565
{
6666
if (resetOwnership)
6767
_syncOwned = false;
68-
var valueM = value as ModificationSynchronizer;
69-
if (valueM != null)
68+
if (value is ModificationSynchronizer valueM)
7069
valueM.Modified += OnModified;
7170
var old = _sync;
7271
_sync = value;
73-
var oldM = old as ModificationSynchronizer;
74-
if (oldM != null)
72+
if (old is ModificationSynchronizer oldM)
7573
oldM.Modified -= OnModified;
7674
return old != value;
7775
}
7876

7977
void SetSyncSynced(IModificationSynchronizer value)
8078
{
81-
var sync = _sync as ModificationSynchronizer;
82-
if (sync != null)
79+
if (_sync is ModificationSynchronizer sync)
8380
{
8481
bool owned = false;
8582
// Allow for wrap-up.
@@ -126,8 +123,7 @@ protected virtual void OnModified()
126123

127124
public void Freeze()
128125
{
129-
var sync = _sync as ModificationSynchronizer;
130-
if (sync != null)
126+
if (_sync is ModificationSynchronizer sync)
131127
{
132128
// Allow for wrap-up.
133129
sync.Modifying(() =>

ModificationSynchronizer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
using Open.Disposable;
12
using System;
23
using System.Threading;
3-
using Open.Disposable;
44

55
namespace Open.Threading
66
{
@@ -117,20 +117,20 @@ protected override void OnDispose(bool calledExplicitly)
117117

118118
public virtual void Reading(Action action)
119119
{
120-
AssertIsAlive();
120+
AssertIsAlive();
121121
action();
122122
}
123123

124124
public virtual T Reading<T>(Func<T> action)
125125
{
126-
AssertIsAlive();
126+
AssertIsAlive();
127127
return action();
128128
}
129129

130130
protected void SignalModified()
131131
{
132-
Modified?.Invoke(this, EventArgs.Empty);
133-
}
132+
Modified?.Invoke(this, EventArgs.Empty);
133+
}
134134

135135
public bool Modifying(Func<bool> action)
136136
{
@@ -149,7 +149,7 @@ public bool Modifying(Action action, bool assumeChange = false)
149149

150150
public virtual bool Modifying(Func<bool> condition, Func<bool> action)
151151
{
152-
AssertIsAlive();
152+
AssertIsAlive();
153153
if (condition != null && !condition())
154154
return false;
155155

@@ -165,7 +165,7 @@ public virtual bool Modifying(Func<bool> condition, Func<bool> action)
165165

166166
public virtual bool Modifying<T>(ref T target, T newValue)
167167
{
168-
AssertIsAlive();
168+
AssertIsAlive();
169169
if (target.Equals(newValue)) return false;
170170

171171
IncrementVersion();
@@ -190,7 +190,7 @@ public SimpleLockingModificationSynchronizer(object sync = null)
190190

191191
public override void Reading(Action action)
192192
{
193-
AssertIsAlive();
193+
AssertIsAlive();
194194
lock (_sync) action();
195195
}
196196

@@ -329,4 +329,4 @@ public override bool Modifying<T>(ref T target, T newValue)
329329

330330
}
331331

332-
}
332+
}

Progress.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace Open.Threading
55
{
66
public class Progress
77
{
8-
public Progress() {
8+
public Progress()
9+
{
910
Index = 0;
1011
Count = 0;
1112

@@ -66,8 +67,8 @@ public void Start(int? newCount = null)
6667

6768
public void Run(Action closure, bool propagateException = false)
6869
{
69-
if(closure==null)
70-
throw new ArgumentNullException("closure");
70+
if (closure == null)
71+
throw new ArgumentNullException(nameof(closure));
7172

7273
Start();
7374
try
@@ -85,8 +86,8 @@ public void Run(Action closure, bool propagateException = false)
8586

8687
public T Execute<T>(Func<T> query, bool propagateException = false)
8788
{
88-
if(query==null)
89-
throw new ArgumentNullException("query");
89+
if (query == null)
90+
throw new ArgumentNullException(nameof(query));
9091

9192
Start();
9293
try
@@ -95,7 +96,7 @@ public T Execute<T>(Func<T> query, bool propagateException = false)
9596
Finish();
9697
return result;
9798
}
98-
catch(Exception ex)
99+
catch (Exception ex)
99100
{
100101
Failed(ex.ToString());
101102
if (propagateException)
@@ -127,7 +128,7 @@ public double Value
127128
if (Count == 0)
128129
return 0; // Signify it hasn'T started.
129130

130-
return (double) Index / Count;
131+
return (double)Index / Count;
131132
}
132133
}
133134

@@ -145,9 +146,9 @@ public string Message
145146

146147
public void Failed(Exception ex)
147148
{
148-
if(ex==null)
149-
throw new ArgumentNullException("ex");
150-
149+
if (ex == null)
150+
throw new ArgumentNullException(nameof(ex));
151+
151152
Failed(ex.ToString());
152153
}
153154

@@ -188,7 +189,7 @@ public TimeSpan EstimatedTimeLeft
188189
{
189190
get
190191
{
191-
if(Index==0 || Count==0)
192+
if (Index == 0 || Count == 0)
192193
return TimeSpan.MaxValue;
193194

194195
var remaining = Count - Index;
@@ -205,7 +206,7 @@ public string EstimatedTimeLeftString
205206
{
206207
get
207208
{
208-
return EstimatedTimeLeft.ToString();//.ToStringVerbose();
209+
return EstimatedTimeLeft.ToString();//.ToStringVerbose();
209210
}
210211
}
211212

0 commit comments

Comments
 (0)