Skip to content

Commit 6674e46

Browse files
committed
private workers
1 parent 26dea0e commit 6674e46

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

GrasshopperAsyncComponent/GH_AsyncComponent.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace GrasshopperAsyncComponent;
77

8-
public sealed class Worker<T> : IDisposable
8+
internal sealed class Worker<T> : IDisposable
99
where T : GH_Component
1010
{
1111
public required WorkerInstance<T> Instance { get; init; }
@@ -46,7 +46,7 @@ public abstract class GH_AsyncComponent<T> : GH_Component, IDisposable
4646
//JEDD: boolean, 1 if this class needs to set the data of the workers...
4747
private volatile int _setData;
4848

49-
public List<Worker<T>> Workers { get; }
49+
private readonly List<Worker<T>> _workers;
5050

5151
/// <summary>
5252
/// Set this property inside the constructor of your derived component.
@@ -61,7 +61,7 @@ public abstract class GH_AsyncComponent<T> : GH_Component, IDisposable
6161
protected GH_AsyncComponent(string name, string nickname, string description, string category, string subCategory)
6262
: base(name, nickname, description, category, subCategory)
6363
{
64-
Workers = new List<Worker<T>>();
64+
_workers = new List<Worker<T>>();
6565

6666
ProgressReports = new ConcurrentDictionary<string, double>();
6767

@@ -81,12 +81,12 @@ protected GH_AsyncComponent(string name, string nickname, string description, st
8181
private void Done()
8282
{
8383
Interlocked.Increment(ref _state);
84-
if (_state == Workers.Count && _setData == 0)
84+
if (_state == _workers.Count && _setData == 0)
8585
{
8686
Interlocked.Exchange(ref _setData, 1);
8787

8888
// We need to reverse the workers list to set the outputs in the same order as the inputs.
89-
Workers.Reverse();
89+
_workers.Reverse();
9090

9191
Rhino.RhinoApp.InvokeOnUiThread(
9292
(Action)
@@ -100,12 +100,12 @@ private void Done()
100100

101101
public virtual void DisplayProgress(object sender, System.Timers.ElapsedEventArgs e)
102102
{
103-
if (Workers.Count == 0 || ProgressReports.Values.Count == 0)
103+
if (_workers.Count == 0 || ProgressReports.Values.Count == 0)
104104
{
105105
return;
106106
}
107107

108-
if (Workers.Count == 1)
108+
if (_workers.Count == 1)
109109
{
110110
Message = ProgressReports.Values.Last().ToString("0.00%");
111111
}
@@ -117,7 +117,7 @@ public virtual void DisplayProgress(object sender, System.Timers.ElapsedEventArg
117117
total += kvp.Value;
118118
}
119119

120-
Message = (total / Workers.Count).ToString("0.00%");
120+
Message = (total / _workers.Count).ToString("0.00%");
121121
}
122122

123123
Rhino.RhinoApp.InvokeOnUiThread(
@@ -138,7 +138,7 @@ protected override void BeforeSolveInstance()
138138

139139
Debug.WriteLine("Killing");
140140

141-
foreach (var currentWorker in Workers)
141+
foreach (var currentWorker in _workers)
142142
{
143143
currentWorker.Cancel();
144144
}
@@ -148,12 +148,12 @@ protected override void BeforeSolveInstance()
148148

149149
protected override void AfterSolveInstance()
150150
{
151-
Debug.WriteLine("After solve instance was called " + _state + " ? " + Workers.Count);
151+
Debug.WriteLine("After solve instance was called " + _state + " ? " + _workers.Count);
152152
// We need to start all the tasks as close as possible to each other.
153-
if (_state == 0 && Workers.Count > 0 && _setData == 0)
153+
if (_state == 0 && _workers.Count > 0 && _setData == 0)
154154
{
155155
Debug.WriteLine("After solve INVOCATION");
156-
foreach (var worker in Workers)
156+
foreach (var worker in _workers)
157157
{
158158
worker.Task.Start();
159159
}
@@ -198,7 +198,7 @@ protected override void SolveInstance(IGH_DataAccess da)
198198
);
199199

200200
// Add the worker to our list
201-
Workers.Add(
201+
_workers.Add(
202202
new()
203203
{
204204
Instance = currentWorker,
@@ -215,18 +215,18 @@ protected override void SolveInstance(IGH_DataAccess da)
215215
return;
216216
}
217217

218-
if (Workers.Count > 0)
218+
if (_workers.Count > 0)
219219
{
220220
Interlocked.Decrement(ref _state);
221-
Workers[_state].Instance.SetData(da);
221+
_workers[_state].Instance.SetData(da);
222222
}
223223

224224
if (_state != 0)
225225
{
226226
return;
227227
}
228228

229-
foreach (var worker in Workers)
229+
foreach (var worker in _workers)
230230
{
231231
worker?.Dispose();
232232
}
@@ -239,7 +239,7 @@ protected override void SolveInstance(IGH_DataAccess da)
239239

240240
private void ResetState()
241241
{
242-
Workers.Clear();
242+
_workers.Clear();
243243
ProgressReports.Clear();
244244

245245
Interlocked.Exchange(ref _state, 0);
@@ -248,7 +248,7 @@ private void ResetState()
248248

249249
public void RequestCancellation()
250250
{
251-
foreach (var worker in Workers)
251+
foreach (var worker in _workers)
252252
{
253253
worker.Cancel();
254254
}
@@ -274,7 +274,7 @@ protected virtual void Dispose(bool disposing)
274274

275275
if (disposing)
276276
{
277-
foreach (var worker in Workers)
277+
foreach (var worker in _workers)
278278
{
279279
worker?.Dispose();
280280
}

0 commit comments

Comments
 (0)