Skip to content

Commit 0afb444

Browse files
authored
Merge pull request #12 from samunohito/feature/#6_ex_component
DisposableComponentのNuGet化 #6
2 parents f2d13ad + 4a70989 commit 0afb444

30 files changed

+222
-253
lines changed

.idea/.idea.SimpleVolumeMixer/.idea/misc.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SimpleVolumeMixer/Core/Helper/Component/DisposableComponent.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

SimpleVolumeMixer/Core/Helper/Component/IDisposableComponent.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

SimpleVolumeMixer/Core/Helper/Component/KeyValueInstanceManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using DisposableComponents;
34

45
namespace SimpleVolumeMixer.Core.Helper.Component;
56

SimpleVolumeMixer/Core/Helper/Component/PollingMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ public void Refresh()
161161
/// <inheritdoc cref="IDisposable.Dispose"/>
162162
public void Dispose()
163163
{
164-
_timer.Stop();
165164
_timer.Elapsed -= TimerOnElapsed;
165+
_timer.Stop();
166166

167167
_disposable.Dispose();
168168
}

SimpleVolumeMixer/Core/Helper/Component/QueueProcessor.cs

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Concurrent;
33
using System.Threading;
44
using System.Threading.Tasks;
5+
using DisposableComponents;
6+
using Microsoft.Extensions.Logging;
57
using Reactive.Bindings.Extensions;
68

79
namespace SimpleVolumeMixer.Core.Helper.Component;
@@ -13,39 +15,28 @@ namespace SimpleVolumeMixer.Core.Helper.Component;
1315
/// <typeparam name="TR">Type of object returned as the return value of Func</typeparam>
1416
public class QueueProcessor<TP, TR> : DisposableComponent
1517
{
18+
private readonly string _name;
19+
private readonly ILogger _logger;
1620
private readonly BlockingCollection<QueueProcessorItem<TP, TR>> _items;
1721
private readonly Task _loopTask;
1822
private readonly CancellationTokenSource _cancellation;
1923

2024
/// <summary>
2125
/// ctor
2226
/// </summary>
27+
/// <param name="name"></param>
28+
/// <param name="logger"></param>
2329
/// <param name="capacity">Maximum number of queues registered</param>
24-
public QueueProcessor(int capacity = 4096)
30+
public QueueProcessor(string name, ILogger logger, int capacity = 4096)
2531
{
32+
_name = name;
33+
_logger = logger;
2634
_items = new BlockingCollection<QueueProcessorItem<TP, TR>>(capacity).AddTo(Disposable);
27-
_loopTask = new Task(() => DoProcess()).AddTo(Disposable);
2835
_cancellation = new CancellationTokenSource().AddTo(Disposable);
29-
}
30-
31-
/// <summary>
32-
/// Starts a worker thread that executes the Func registered in the Queue.
33-
/// The thread that invokes this method will not be blocked.
34-
/// </summary>
35-
public void StartRequest()
36-
{
36+
_loopTask = new Task(() => DoProcess(), _cancellation.Token).AddTo(Disposable);
3737
_loopTask.Start();
3838
}
3939

40-
/// <summary>
41-
/// Sends a stop request to the worker thread.
42-
/// Note that this method call does not necessarily stop the worker thread immediately.
43-
/// </summary>
44-
public void StopRequest()
45-
{
46-
_cancellation.Cancel(false);
47-
}
48-
4940
/// <summary>
5041
/// Register Func (and its auxiliary objects) in the queue.
5142
/// When the number of registrations in the queue reaches the capacity set in the constructor,
@@ -54,6 +45,11 @@ public void StopRequest()
5445
/// <param name="item"></param>
5546
public void Push(QueueProcessorItem<TP, TR> item)
5647
{
48+
if (IsDisposed)
49+
{
50+
return;
51+
}
52+
5753
_items.Add(item);
5854
}
5955

@@ -62,24 +58,55 @@ public void Push(QueueProcessorItem<TP, TR> item)
6258
/// </summary>
6359
private void DoProcess()
6460
{
61+
_logger.LogInformation("[{}] start looping...", _name);
62+
6563
while (!_cancellation.IsCancellationRequested)
6664
{
67-
var item = _items.Take();
68-
if (item.CancelRequest)
65+
try
6966
{
67+
var item = _items.Take(_cancellation.Token);
68+
if (item.CancelRequest)
69+
{
70+
item.Executed = true;
71+
continue;
72+
}
73+
74+
item.Result = item.Function(item.Argument);
7075
item.Executed = true;
71-
continue;
7276
}
73-
74-
item.Result = item.Function(item.Argument);
75-
item.Executed = true;
77+
catch (OperationCanceledException ex)
78+
{
79+
_logger.LogError(ex, "[{}] canceled", _name);
80+
}
7681
}
82+
83+
_logger.LogInformation("[{}] finish looping...", _name);
7784
}
7885

7986
/// <inheritdoc cref="DisposableComponent.OnDisposing"/>
8087
protected override void OnDisposing()
8188
{
82-
StopRequest();
89+
_logger.LogInformation("[{}] disposing...", _name);
90+
91+
_cancellation.Cancel(false);
92+
93+
switch (_loopTask.Status)
94+
{
95+
case TaskStatus.Canceled:
96+
case TaskStatus.Faulted:
97+
case TaskStatus.RanToCompletion:
98+
break;
99+
default:
100+
_loopTask.Wait();
101+
break;
102+
}
103+
83104
base.OnDisposing();
84105
}
106+
107+
protected override void OnDisposed()
108+
{
109+
_logger.LogInformation("[{}] disposed...", _name);
110+
base.OnDisposed();
111+
}
85112
}

SimpleVolumeMixer/Core/Helper/Component/SafetyAccessorComponent.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

SimpleVolumeMixer/Core/Helper/Component/SynchronizedObservableCollectionWrapper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Collections;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.Reactive.Concurrency;
5+
using System.Threading;
6+
using DisposableComponents;
47
using Reactive.Bindings;
58
using Reactive.Bindings.Extensions;
69

0 commit comments

Comments
 (0)