12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- namespace Universal;
- public class ParallelHelper : IDisposable
- {
- private readonly List<Task> _tasks = [];
- private bool _isStarted = false;
- void IDisposable.Dispose()
- {
- _tasks.ForEach(task => task.Dispose());
- _tasks.Clear();
- _isStarted = false;
- }
- public bool Insert(Action action)
- {
- if (_isStarted)
- return false;
- Task task = new(action);
- _tasks.Add(task);
- return true;
- }
- public bool StartnWaitAll()
- {
- if (_isStarted)
- return false;
- _tasks.ForEach(t => t.Start());
- Task.WhenAll(_tasks).Wait();
- _isStarted = false;
- _tasks.Clear();
- return true;
- }
- public bool StartnWaitAny()
- {
- if (_isStarted)
- return false;
- _tasks.ForEach(t => t.Start());
- Task.WhenAny(_tasks).Wait();
- return true;
- }
- }
|