C#: Create CPU Usage at Custom Percentage

This then?

    DateTime lastSleep = DateTime.Now;            
    while (true)
    {
        TimeSpan span = DateTime.Now - lastSleep;
        if (span.TotalMilliseconds > 700)
        {
            Thread.Sleep(300);
            lastSleep = DateTime.Now;
        }
    }

You could use smaller numbers to get a more steady load....as long as the ratio is whatever you want. This does only use one core though, so you might have to do this in multiple threads.


You could add a threaded timer that wakes up on an interval and does some work. Then tweak the interval and amount of work until you approximate the load you want.

    private void button1_Click(object sender, EventArgs e)
    {
        m_timer = new Timer(DoWork);
        m_timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(10));
    }

    private static void DoWork(object state)
    {
        long j = 0;
        for (int i = 0; i < 2000000; i++)
        {
            j += 1;
        }
        Console.WriteLine(j);
    }

alt text

With that and tweaking the value of the loop I was able to add 20%, 60% and full load to my system. It will scale for multiple cores using additional threads for more even load.


The utility provides a simple slider bar user interface that allows you to place an arbitrary load on the processor(s) in your system. Automatically detects and handles multiple processors.

Worked very well for me when I downloaded it this morning.