Skip to content

Commit 9520fba

Browse files
author
Oren (electricessence)
committed
Updates after resharper inspection.
1 parent 0cb86fc commit 9520fba

6 files changed

Lines changed: 47 additions & 61 deletions

File tree

ActionRunner.cs

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,27 @@ public static ActionRunner Create<T>(Func<T> action, TaskScheduler scheduler = n
2525
}
2626

2727
Action _action;
28+
// ReSharper disable once NotAccessedField.Global
2829
protected TaskScheduler _scheduler;
2930

3031
protected int _count;
31-
public int Count
32-
{
33-
get { return _count; }
34-
}
32+
public int Count => _count;
3533

3634
public DateTime LastStart
3735
{
3836
get;
3937
protected set;
4038
}
4139

42-
public bool HasBeenRun
43-
{
44-
get
45-
{
46-
return LastStart < DateTime.Now;
47-
}
48-
}
40+
public bool HasBeenRun => LastStart < DateTime.Now;
4941

5042
public DateTime LastComplete
5143
{
5244
get;
5345
protected set;
5446
}
5547

56-
public bool HasCompleted
57-
{
58-
get
59-
{
60-
return LastComplete < DateTime.Now;
61-
}
62-
}
63-
64-
public Exception LastFault
65-
{
66-
get;
67-
protected set;
68-
}
48+
public bool HasCompleted => LastComplete < DateTime.Now;
6949

7050
public bool Cancel(bool onlyIfNotRunning)
7151
{
@@ -84,6 +64,7 @@ public void Dispose()
8464
{
8565
Cancel();
8666
_action = null;
67+
_scheduler = null;
8768
}
8869

8970
Action GetAction()
@@ -94,13 +75,7 @@ Action GetAction()
9475
return a;
9576
}
9677

97-
public bool IsScheduled
98-
{
99-
get
100-
{
101-
return _task?.IsActive() ?? false;
102-
}
103-
}
78+
public bool IsScheduled => _task?.IsActive() ?? false;
10479

10580
/// <summary>
10681
/// Indiscriminately invokes the action.
@@ -110,6 +85,7 @@ public void RunSynchronously()
11085
GetAction().Invoke();
11186
}
11287

88+
// ReSharper disable once UnusedMember.Local
11389
readonly object _taskLock = new object();
11490
CancellableTask _task;
11591
CancellableTask Prepare()
@@ -119,7 +95,6 @@ CancellableTask Prepare()
11995
task
12096
.OnFaulted(ex =>
12197
{
122-
LastFault = ex;
12398
})
12499
.OnFullfilled(() =>
125100
{
@@ -145,14 +120,12 @@ public CancellableTask Defer(TimeSpan delay, bool clearSchedule = true)
145120
Cancel(true); // Don't cancel defered if already running.
146121
}
147122

148-
CancellableTask task = null;
149-
if ((task = _task) == null)
123+
CancellableTask task;
124+
if ((task = _task) != null) return task;
125+
task = Prepare();
126+
if (null == Interlocked.CompareExchange(ref _task, task, null))
150127
{
151-
task = Prepare();
152-
if (null == Interlocked.CompareExchange(ref _task, task, null))
153-
{
154-
task.Start(delay);
155-
}
128+
task.Start(delay);
156129
}
157130
return task;
158131
}

CancellableTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void Start(TimeSpan delay, TaskScheduler scheduler = null)
8484
}
8585
else
8686
{
87-
int runState = 0;
87+
var runState = 0;
8888

8989
ContinueWith(t =>
9090
{

ICancellable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace Open.Threading.Tasks
45
{
6+
[SuppressMessage("ReSharper", "UnusedMemberInSuper.Global")]
57
public interface ICancellable : IDisposable
68
{
79
/// <summary>

Open.Threading.Tasks.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ Part of the "Open" set of libraries.</Description>
1414
<PackageLicenseUrl>https://github.com/electricessence/Open.Threading.Tasks/blob/master/LISCENSE.md</PackageLicenseUrl>
1515
<RepositoryType>git</RepositoryType>
1616
<PackageTags>dotnet, dotnet-core, dotnetcore, cs, extensions, actionrunner, cancellable, cancellabletask, progress, task-extensions</PackageTags>
17-
<Version>1.1.2</Version>
17+
<Version>1.1.3</Version>
1818
<AssemblyVersion>1.1.2.0</AssemblyVersion>
1919
<FileVersion>1.1.2.0</FileVersion>
2020
<PackageReleaseNotes></PackageReleaseNotes>
2121
</PropertyGroup>
2222

2323
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
2424
<Optimize>True</Optimize>
25+
<LangVersion>latest</LangVersion>
26+
</PropertyGroup>
27+
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
29+
<LangVersion>latest</LangVersion>
2530
</PropertyGroup>
2631

2732
<ItemGroup>

TaskExtensions.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,28 @@ public static bool IsActive(this Task target)
3333
/// <summary>
3434
/// Checks the status of the task and attempts to start it if waiting to start (TaskStatus.Created).
3535
/// </summary>
36+
/// <param name="target">The task to ensure start.</param>
3637
/// <param name="scheduler">Optional scheduler to use.</param>
3738
/// <returns>True if start attempt was successful.</returns>
3839
public static bool EnsureStarted(this Task target, TaskScheduler scheduler = null)
3940
{
4041
if (target == null) throw new NullReferenceException();
4142

42-
if (target.Status == TaskStatus.Created)
43+
if (target.Status != TaskStatus.Created) return false;
44+
try
4345
{
44-
try
45-
{
46-
if (scheduler == null)
47-
target.Start();
48-
else
49-
target.Start(scheduler);
46+
if (scheduler == null)
47+
target.Start();
48+
else
49+
target.Start(scheduler);
5050

51-
return true;
52-
}
53-
catch (InvalidOperationException)
54-
{
55-
// Even though we've checked the status, it's possible it could have been started. We can't guarantee proper handling without a trap here.
56-
if (target.Status == TaskStatus.Created)
57-
throw; // Something wierd must have happened if we arrived here.
58-
}
51+
return true;
52+
}
53+
catch (InvalidOperationException)
54+
{
55+
// Even though we've checked the status, it's possible it could have been started. We can't guarantee proper handling without a trap here.
56+
if (target.Status == TaskStatus.Created)
57+
throw; // Something wierd must have happened if we arrived here.
5958
}
6059

6160
return false;
@@ -65,6 +64,7 @@ public static bool EnsureStarted(this Task target, TaskScheduler scheduler = nul
6564
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if completed and not cancelled.
6665
/// </summary>
6766
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
67+
/// <param name="target">The task.</param>
6868
/// <param name="action">The action to perform if fullfulled.</param>
6969
/// <returns>The target object. Allows for method chaining.</returns>
7070
public static TTask OnFullfilled<TTask>(this TTask target, Action action)
@@ -81,7 +81,8 @@ public static TTask OnFullfilled<TTask>(this TTask target, Action action)
8181
/// <summary>
8282
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if completed and not cancelled.
8383
/// </summary>
84-
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
84+
/// <typeparam name="T">The return type is the same as the target.</typeparam>
85+
/// <param name="target">The task.</param>
8586
/// <param name="action">The action to perform if fullfulled.</param>
8687
/// <returns>The target object. Allows for method chaining.</returns>
8788
public static Task<T> OnFullfilled<T>(this Task<T> target, Action<T> action)
@@ -97,7 +98,9 @@ public static Task<T> OnFullfilled<T>(this Task<T> target, Action<T> action)
9798
/// <summary>
9899
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if completed and not cancelled.
99100
/// </summary>
100-
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
101+
/// <typeparam name="TTask">The task type.</typeparam>
102+
/// <typeparam name="T">The return type of the task.</typeparam>
103+
/// <param name="target">The task.</param>
101104
/// <param name="action">The action to perform if fullfulled.</param>
102105
/// <returns>The target object. Allows for method chaining.</returns>
103106
public static TTask OnFullfilled<TTask, T>(this TTask target, Func<T> action)
@@ -115,6 +118,7 @@ public static TTask OnFullfilled<TTask, T>(this TTask target, Func<T> action)
115118
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if faulted.
116119
/// </summary>
117120
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
121+
/// <param name="target">The task.</param>
118122
/// <param name="action">The action to perform if faulted.</param>
119123
/// <returns>The target object. Allows for method chaining.</returns>
120124
public static TTask OnFaulted<TTask>(this TTask target, Action<Exception> action)
@@ -133,6 +137,7 @@ public static TTask OnFaulted<TTask>(this TTask target, Action<Exception> action
133137
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if cancelled.
134138
/// </summary>
135139
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
140+
/// <param name="target">The task.</param>
136141
/// <param name="action">The action to perform if cancelled.</param>
137142
/// <returns>The target object. Allows for method chaining.</returns>
138143
public static TTask OnCancelled<TTask>(this TTask target, Action action)
@@ -149,7 +154,9 @@ public static TTask OnCancelled<TTask>(this TTask target, Action action)
149154
/// <summary>
150155
/// Utility method that can be chained with other methods for reacting to Task results. Only invokes the action if cancelled.
151156
/// </summary>
152-
/// <typeparam name="TTask">The return type is the same as the target.</typeparam>
157+
/// <typeparam name="TTask">The task type.</typeparam>
158+
/// <typeparam name="T">The return type of the task.</typeparam>
159+
/// <param name="target">The task.</param>
153160
/// <param name="action">The action to perform if cancelled.</param>
154161
/// <returns>The target object. Allows for method chaining.</returns>
155162
public static TTask OnCancelled<TTask, T>(this TTask target, Func<T> action)

TimeoutHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ namespace Open.Threading.Tasks
66
{
77
public class TimeoutHandler : IDisposable
88
{
9-
10-
CancellationTokenSource TokenSource;
9+
readonly CancellationTokenSource TokenSource;
1110
TimeoutHandler(int delay, Action<int> onComplete)
1211
{
1312
TokenSource = new CancellationTokenSource();

0 commit comments

Comments
 (0)