laatzen/Common/CommonCore/ThreadWatcher/ThreadWatcher.cs

63 lines
1.4 KiB
C#
Raw Normal View History

2021-10-01 09:09:20 +00:00
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Xylem.Common.CommonCore.ThreadWatcher
{
public sealed class TaskWatcher
{
#region Singleton
private static readonly Lazy<TaskWatcher>
2022-08-24 10:01:41 +00:00
Lazy =
2021-10-01 09:09:20 +00:00
new Lazy<TaskWatcher>
(() => new TaskWatcher());
2022-08-24 10:01:41 +00:00
public static TaskWatcher Instance => Lazy.Value;
2021-10-01 09:09:20 +00:00
#endregion
private TaskWatcher()
{
2022-08-24 10:01:41 +00:00
_tasks = new ConcurrentDictionary<Guid, Task>();
2021-10-01 09:09:20 +00:00
}
2022-08-24 10:01:41 +00:00
private readonly ConcurrentDictionary<Guid, Task> _tasks;
2021-10-01 09:09:20 +00:00
public void Add(Task t)
{
2022-08-24 10:01:41 +00:00
_tasks.TryAdd(Guid.NewGuid(), t);
2021-10-01 09:09:20 +00:00
}
}
public sealed class ThreadWatcher
{
#region Singleton
private static readonly Lazy<ThreadWatcher>
2022-08-24 10:01:41 +00:00
Lazy =
2021-10-01 09:09:20 +00:00
new Lazy<ThreadWatcher>
(() => new ThreadWatcher());
2022-08-24 10:01:41 +00:00
public static ThreadWatcher Instance => Lazy.Value;
2021-10-01 09:09:20 +00:00
#endregion
private ThreadWatcher()
{
2022-08-24 10:01:41 +00:00
_threads = new ConcurrentDictionary<Guid, Thread>();
2021-10-01 09:09:20 +00:00
}
2022-08-24 10:01:41 +00:00
private readonly ConcurrentDictionary<Guid,Thread> _threads;
2021-10-01 09:09:20 +00:00
public void Start(Thread t)
{
t.Start();
2022-08-24 10:01:41 +00:00
_threads.TryAdd(Guid.NewGuid(), t);
2021-10-01 09:09:20 +00:00
}
}
}