यहाँ एक सरल कार्यक्रम है कि 5 धागे कि CPU के विभिन्न मात्रा में उपभोग की शुरूआत है और तब मेल खाता है कि प्रबंधित थ्रेड कितना सीपीयू उपभोग कर रहा है।
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();
static void Main(string[] args)
{
Dictionary<int, Thread> threads = new Dictionary<int, Thread>();
// Launch the threads
for (int i = 0; i < 5; i++)
{
Thread cpuThread = new Thread((start) =>
{
lock (threads)
{
threads.Add(GetCurrentWin32ThreadId(), Thread.CurrentThread);
}
ConsumeCPU(20 * (int)start);
});
cpuThread.Name = "T" + i;
cpuThread.Start(i);
}
// Every second wake up and see how much CPU each thread is using.
Thread monitoringThread = new Thread(() =>
{
Stopwatch watch = new Stopwatch();
watch.Start();
while (true)
{
Thread.Sleep(1000);
Console.Write("\r");
double totalTime = ((double)watch.ElapsedMilliseconds);
if (totalTime > 0)
{
Process p = Process.GetCurrentProcess();
foreach (ProcessThread pt in p.Threads)
{
Thread managedThread;
if (threads.TryGetValue(pt.Id, out managedThread))
{
double percent = (pt.TotalProcessorTime.TotalMilliseconds/totalTime);
Console.Write("{0}-{1:0.00} ", managedThread.Name, percent);
}
}
}
}
});
monitoringThread.Start();
}
// Helper function that generates a percentage of CPU usage
public static void ConsumeCPU(int percentage)
{
Stopwatch watch = new Stopwatch();
watch.Start();
while (true)
{
if (watch.ElapsedMilliseconds > percentage)
{
Thread.Sleep(100 - percentage);
watch.Reset();
watch.Start();
}
}
}
}
ध्यान दें कि यह संभव है कि CLR देशी धागा कि कामयाब धागा के तहत निष्पादित हो रहा है बदल जाएगा। हालांकि, अभ्यास में मुझे यकीन नहीं है कि यह वास्तव में कितनी बार होता है।
स्रोत
2017-01-10 20:56:19
यह उतना करीब है जितना मुझे लगता है कि हम प्राप्त करने जा रहे हैं ... धन्यवाद! –
लिंक अब काम नहीं करता –