Show / Hide Table of Contents

Sample: IntroStaThread

If the code you want to benchmark requires [System.STAThread] then you need to apply this attribute to the benchmarked method. BenchmarkDotNet will generate executable with [STAThread] applied to it's Main method.

Currently it does not work for .NET Core 2.0 due to this bug.

Source code

using System.Threading;
using BenchmarkDotNet.Attributes;

namespace BenchmarkDotNet.Samples
{
    public class IntroStaThread
    {
        [Benchmark, System.STAThread]
        public void CheckForSTA()
        {
            if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
            {
                throw new ThreadStateException(
                    "The current threads apartment state is not STA");
            }
        }
    }
}

Links

  • Customizing Runtime
  • The permanent link to this sample: Sample: IntroStaThread

  • Improve this Doc
In This Article
Back to top Copyright © 2013–2021 .NET Foundation and contributors