laatzen/Common/CommonCore/ThreadWatcher/ThreadWatcher.cs
2021-10-01 11:09:20 +02:00

63 lines
1.5 KiB
C#

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>
lazy =
new Lazy<TaskWatcher>
(() => new TaskWatcher());
public static TaskWatcher Instance { get { return lazy.Value; } }
#endregion
private TaskWatcher()
{
tasks = new ConcurrentDictionary<Guid, Task>();
}
private ConcurrentDictionary<Guid, Task> tasks;
public void Add(Task t)
{
tasks.TryAdd(Guid.NewGuid(), t);
}
}
public sealed class ThreadWatcher
{
#region Singleton
private static readonly Lazy<ThreadWatcher>
lazy =
new Lazy<ThreadWatcher>
(() => new ThreadWatcher());
public static ThreadWatcher Instance { get { return lazy.Value; } }
#endregion
private ThreadWatcher()
{
threads = new ConcurrentDictionary<Guid, Thread>();
tasks = new ConcurrentDictionary<Guid, Task>();
}
private ConcurrentDictionary<Guid,Thread> threads;
private ConcurrentDictionary<Guid, Task> tasks;
public void Start(Thread t)
{
t.Start();
threads.TryAdd(Guid.NewGuid(), t);
}
}
}