谁能告诉我在 testTimer 之后我需要做什么。开始,因为秒表仍然是下划线 (Visual Studio C#)

Can anyone tell me what i need to do after testTimer. Start since Stopwatch is still underline (Visual Studio C#)

private void btnTestConcatenations_Click(object sender, EventArgs e)
{
    var testTimer = new Stopwatch();
    testTimer.Start();
    testTimer.Stop();
    var elapsedTime = testTimer.Elapsed;

    var strTest = string.Empty;

    for (int loopcount = 0; loopcount < NUMBER_CONCATENATIONS_TO_PERFORM; loopcount++)
    {
        strTest += "Adding 20 caracters";
    }

    Application.DoEvents();     

秒表用于计时操作。由于此方法中唯一发生的其他事情是连接循环,可以安全地假设这就是您想要的时间吗?

如果是这样,你会这样做:

private void btnTestConcatenations_Click(object sender, EventArgs e)
{
    var testTimer = new Stopwatch();
    var strTest = string.Empty;
    var numOperations = NUMBER_CONCATENATIONS_TO_PERFORM;

    // Start the stopwatch
    testTimer.Start();

    // Do some operation that you want to measure
    for (int loopcount = 0; loopcount < numOperations; loopcount++)
    {
        strTest += "Adding 20 characters";
    }

    // Stop the stopwatch
    testTimer.Stop();
    var elapsedTime = testTimer.Elapsed;

    // Do something with the stopwatch results
    MessageBox.Show($"It took {elapsedTime} seconds to do {numOperations} concatenations");
}