Trying to solve a core affinity problem results in UI stalling under heavy CPU load

Solution 1:

You're setting the CPU affinity for the UI thread, then running the test routine on the same thread so it makes sense your UI is hanging during the test. Simplify things and ensure your UI/threading is working properly before you jump into actually performing your test routine.

private int TestingMethod()
{
    // set affinity for current thread here when ready

    // mock a busy thread by sleeping
    System.Threading.Thread.Sleep( 15 * 1000 );

    return 42;
}

// don't use `async void`
private async Task PerCoreTest()
{
    TestLabel.Text = "Running...";

    // we're in the UI thread, so we want to run
    // the test in another thread.  run a new
    // task to do so, await result so the continuation
    // will execute back in the UI thread
    var score = await Task.Run(() => TestingMethod());

    TestLabel.Text = score.ToString();
}

private async Task TestButton_Click(object sender, EventArgs e)
{
    await PerCoreTest();
}

Nice and simple. Add something else to the form that updates every second or so or a button you can click to verify the UI is updating properly as the test routine is running.

Once you've verified that the UI isn't locking up, then you may begin adding substance to your test routine. I suggest just getting a working test routine without processor affinity first.

private int TestingMethod()
{
    var score = 0;

    // set affinity for current thread here when ready

    do
    {
        // your cpu-frying logic
    }
    while( /* sentinel condition */ )

    return score;
}

Again, verify the UI is responsive during the test and you can also verify one of your cores is getting abused. Once all that is verified, you may then set the thread affinity INSIDE the TestingMethod() method's implementation (abstracting it to another method call is fine as well as long as it's called from within the TestingMethod's body and isn't run in a Task or another thread. You can pass the mask into TestingMethod as a parameter from the PerCoreTest method.

This should get you on the right track to doing what you want to do. I suggest you spend some quality time reading about multithreading in general and .NET's threading/asynchronous programming model if you plan on continuing with it in the future.