Cause
Using Thread.CurrentThread.ManagedThreadId for getting the current managed thread ID instead of System.Environment.CurrentManagedThreadId.
Rule description
System.Environment.CurrentManagedThreadId is a compact and efficient replacement of the Thread.CurrentThread.ManagedThreadId pattern.
How to fix violations
The violation can either be fixed manually, or, in some cases, using Quick Actions to fix code in Visual Studio.
The following two code snippets show a violation of the rule and how to fix it:
using System.Threading;
class MyClass
{
void MyMethod()
{
int id = Thread.CurrentThread.ManagedThreadId; // Violation occurs
}
}Imports System.Threading
Class MyClass
Private Sub MyMethod()
Dim id As Integer = Thread.CurrentThread.ManagedThreadId ' Violation occurs.
End Function
End Classusing System.Threading;
class MyClass
{
void MyMethod()
{
int id = System.Environment.CurrentManagedThreadId; // Violation fixed
}
}Imports System.Threading
Class MyClass
Private Sub MyMethod()
Dim id As Integer = System.Environment.CurrentManagedThreadId ' Violation fixed.
End Function
End ClassWhen to suppress warnings
It's safe to suppress a violation of this rule if you're not concerned about the performance impact from using Thread.CurrentThread.ManagedThreadId.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA1840
// The code that's violating the rule is on this line.
#pragma warning restore CA1840To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1840.severity = noneFor more information, see How to suppress code analysis warnings.