0

I'm currently in a need to develop CPU stress testing functionality with UWP application. It will later be integrated into main application, currently developing it in a dedicated test "app". Basically I need functionality to set cpu usage to 100% and back to normal when user requests. Currently Event handlers can be simple button_clock handlers, like this:

private void StawpButton_Click(object sender, RoutedEventArgs e)
    {
        //should stop stress-testing somehow...
    }

For burning cores I used This nifty code which uses while(true loop) for stress-testing cpu. However this is rather fire-and-forget method and I dont know how to shut this kond of process down.

My Current Code looks like this:

class CpuStresser
{


    public CpuStresser()
    {
        //do something when initializing
    }
    public static void DoStress()
    {
        for (int i = 0; i < 32; i++)
        {
            Task.Run(() => KillCore());
        }

    }


    public static Task KillCore()
    {
        Random rand = new Random();
        long num = 0;
        while (true)
        {
            num += rand.Next(100, 1000);
            if (num > 1000000) { num = 0; }

        }

    }

}

This works well for putting cpu to 100% load, but I'm unable to shut down tasks. What would be the best way to do it using event handlers?

I tried to add condition to while(true) loop, but error "not all code paths return a value" comes quick. Also making variable that is accessible to both KillCore task and EventHandler is rather difficult, because these are located in different classes.

LempsPC
  • 125
  • 1
  • 1
  • 9

1 Answers1

0
     await Task.Factory.StartNew(() => {KillCore() },
                                       new System.Threading.CancellationToken(),TaskCreationOptions.PreferFairness,TaskScheduler.FromCurrentSynchronizationContext());

you should replace System.Threading.CancellationToken() with your own token once you learn how they work, starting from here.

Xeorge Xeorge
  • 452
  • 4
  • 10