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 an executable with [STAThread]
applied to its Main
method.
Using this feature on .NET Core requires .NET Core 2.1 or newer. Older versions will not work 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: BenchmarkDotNet.Samples.IntroStaThread