63 lines
1.4 KiB
C#
63 lines
1.4 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 => Lazy.Value;
|
|
|
|
#endregion
|
|
private TaskWatcher()
|
|
{
|
|
_tasks = new ConcurrentDictionary<Guid, Task>();
|
|
}
|
|
|
|
|
|
private readonly 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 => Lazy.Value;
|
|
|
|
#endregion
|
|
private ThreadWatcher()
|
|
{
|
|
_threads = new ConcurrentDictionary<Guid, Thread>();
|
|
}
|
|
|
|
private readonly ConcurrentDictionary<Guid,Thread> _threads;
|
|
|
|
|
|
public void Start(Thread t)
|
|
{
|
|
|
|
t.Start();
|
|
|
|
_threads.TryAdd(Guid.NewGuid(), t);
|
|
}
|
|
|
|
}
|
|
}
|