100 lines
3.6 KiB
C#
100 lines
3.6 KiB
C#
// /**
|
|
// * File: CurrentThreadsProvider.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Diagnostics;
|
|
|
|
namespace ln.manage.provider
|
|
{
|
|
public class CurrentThreadsProvider : ManagedObjectProvider
|
|
{
|
|
Dictionary<int, ManagedThread> managedThreads = new Dictionary<int, ManagedThread>();
|
|
|
|
public CurrentThreadsProvider(ManagedObjectProvider container)
|
|
:base(typeof(ManagedThread),"threads",container)
|
|
{
|
|
}
|
|
|
|
public override IEnumerable<object> EnumerateItems()
|
|
{
|
|
HashSet<ManagedThread> currentThreads = new HashSet<ManagedThread>(managedThreads.Values);
|
|
|
|
foreach (ProcessThread processThread in Process.GetCurrentProcess().Threads)
|
|
{
|
|
if (managedThreads.TryGetValue(processThread.Id,out ManagedThread managedThread))
|
|
{
|
|
currentThreads.Remove(managedThread);
|
|
} else
|
|
{
|
|
managedThread = new ManagedThread(this, processThread);
|
|
managedThreads.Add(managedThread.Id, managedThread);
|
|
}
|
|
}
|
|
|
|
foreach (ManagedThread managedThread in currentThreads)
|
|
managedThreads.Remove(managedThread.Id);
|
|
|
|
return managedThreads.Values;
|
|
}
|
|
|
|
public override object GetManagedObject(object uniqueIdentifier)
|
|
{
|
|
int id = (int)uniqueIdentifier;
|
|
if (!managedThreads.ContainsKey(id))
|
|
EnumerateItems();
|
|
return managedThreads[id];
|
|
}
|
|
|
|
public override ManagedObjectState GetManagedObjectState(object o) => (o as ManagedThread).State;
|
|
|
|
public override void Disable(object o) => throw new NotSupportedException();
|
|
public override void Enable(object o) => throw new NotSupportedException();
|
|
public override void RemoveObject(object o) => throw new NotSupportedException();
|
|
|
|
public class ManagedThread
|
|
{
|
|
ProcessThread thread;
|
|
|
|
public ManagedThread(CurrentThreadsProvider provider,ProcessThread thread)
|
|
{
|
|
this.thread = thread;
|
|
}
|
|
|
|
internal ManagedObjectState State
|
|
{
|
|
get
|
|
{
|
|
switch (thread.ThreadState)
|
|
{
|
|
case System.Diagnostics.ThreadState.Running:
|
|
case System.Diagnostics.ThreadState.Wait:
|
|
return ManagedObjectState.ENABLED;
|
|
case System.Diagnostics.ThreadState.Initialized:
|
|
case System.Diagnostics.ThreadState.Ready:
|
|
case System.Diagnostics.ThreadState.Standby:
|
|
case System.Diagnostics.ThreadState.Terminated:
|
|
case System.Diagnostics.ThreadState.Transition:
|
|
return ManagedObjectState.DISABLED;
|
|
default:
|
|
return ManagedObjectState.INVALID;
|
|
}
|
|
}
|
|
}
|
|
|
|
public int Id => thread.Id;
|
|
public int StartAddress => thread.StartAddress.ToInt32();
|
|
public double UserTime => thread.UserProcessorTime.TotalSeconds;
|
|
public double TotalTime => thread.TotalProcessorTime.TotalSeconds;
|
|
}
|
|
}
|
|
|
|
}
|