Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 5333b72

Browse files
Disposable ITaskManager
1 parent 88adbf1 commit 5333b72

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ protected virtual void Dispose(bool disposing)
212212
{
213213
if (disposed) return;
214214
disposed = true;
215-
if (TaskManager != null) TaskManager.Stop();
215+
if (TaskManager != null) TaskManager.Dispose();
216216
if (repositoryManager != null) repositoryManager.Dispose();
217217
}
218218
}

src/GitHub.Api/NewTaskSystem/ITaskManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace GitHub.Unity
56
{
6-
interface ITaskManager
7+
interface ITaskManager : IDisposable
78
{
89
TaskScheduler ConcurrentScheduler { get; }
910
TaskScheduler ExclusiveScheduler { get; }
@@ -15,7 +16,6 @@ interface ITaskManager
1516
T ScheduleConcurrent<T>(T task) where T : ITask;
1617
T ScheduleExclusive<T>(T task) where T : ITask;
1718
T ScheduleUI<T>(T task) where T : ITask;
18-
void Stop();
1919
Task Wait();
2020
}
2121
}

src/GitHub.Api/NewTaskSystem/TaskManager.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TaskManager : ITaskManager
88
{
99
private static readonly ILogging logger = Logging.GetLogger<TaskManager>();
1010

11-
private readonly CancellationTokenSource cts;
11+
private CancellationTokenSource cts;
1212
private readonly ConcurrentExclusiveInterleave manager;
1313
public TaskScheduler UIScheduler { get; set; }
1414
public TaskScheduler ConcurrentScheduler { get { return manager.ConcurrentTaskScheduler; } }
@@ -31,12 +31,6 @@ public TaskManager(TaskScheduler uiScheduler)
3131
this.UIScheduler = uiScheduler;
3232
}
3333

34-
public void Stop()
35-
{
36-
cts.Cancel();
37-
Wait();
38-
}
39-
4034
public Task Wait()
4135
{
4236
return manager.Wait();
@@ -56,7 +50,6 @@ public static TaskScheduler GetScheduler(TaskAffinity affinity)
5650
}
5751
}
5852

59-
6053
public void Schedule(params ITask[] tasks)
6154
{
6255
Guard.ArgumentNotNull(tasks, "tasks");
@@ -157,5 +150,30 @@ private T ScheduleConcurrent<T>(T task, bool setupFaultHandler)
157150
}
158151
return (T)task.Start(manager.ConcurrentTaskScheduler);
159152
}
153+
154+
private void Stop()
155+
{
156+
if (cts == null)
157+
throw new ObjectDisposedException(nameof(TaskManager));
158+
cts.Cancel();
159+
Wait();
160+
cts = null;
161+
}
162+
163+
private bool disposed = false;
164+
private void Dispose(bool disposing)
165+
{
166+
if (disposed) return;
167+
disposed = true;
168+
if (disposing)
169+
{
170+
Stop();
171+
}
172+
}
173+
174+
public void Dispose()
175+
{
176+
Dispose(true);
177+
}
160178
}
161179
}

src/tests/IntegrationTests/BaseIntegrationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public virtual void OnSetup()
4040
[TearDown]
4141
public virtual void OnTearDown()
4242
{
43-
TaskManager.Instance?.Stop();
43+
TaskManager.Instance?.Dispose();
4444
Logger.Debug("Deleting TestBasePath: {0}", TestBasePath.ToString());
4545
for (var i = 0; i < 5; i++)
4646
{

src/tests/TaskSystemIntegrationTests/Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void OneTimeSetup()
5656
[TestFixtureTearDown]
5757
public void OneTimeTearDown()
5858
{
59-
TaskManager?.Stop();
59+
TaskManager?.Dispose();
6060
try
6161
{
6262
TestBasePath.DeleteIfExists();

0 commit comments

Comments
 (0)