-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add doc for analyzer CA1839 and CA1840
- Loading branch information
Showing
7 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
docs/fundamentals/code-analysis/quality-rules/ca1839.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: "CA1839: Use 'Environment.ProcessPath'" | ||
description: "'Environment.ProcessPath' is simpler and faster than 'Process.GetCurrentProcess().MainModule.FileName'." | ||
ms.date: 11/23/2021 | ||
ms.topic: reference | ||
f1_keywords: | ||
- "CA1839" | ||
helpviewer_keywords: | ||
- "CA1839" | ||
author: buyaa-n | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
|
||
# CA1839: Use `Environment.ProcessPath` instead of `Process.GetCurrentProcess().MainModule.FileName` | ||
|
||
| | Value | | ||
|-|-| | ||
| **Rule ID** |CA1839| | ||
| **Category** |[Performance](performance-warnings.md)| | ||
| **Fix is breaking or non-breaking** |Non-breaking| | ||
|
||
## Cause | ||
|
||
Using `Process.GetCurrentProcess().MainModule.FileName` for getting the path to the file that launched the process instead of `Environment.ProcessPath`. | ||
|
||
## Rule description | ||
|
||
`System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName` is expensive: | ||
|
||
- It allocates a <xref:System.Diagnostics.Process> and <xref:System.Diagnostics.ProcessModule> instances, usually just to get the `FileName`. | ||
- The <xref:System.Diagnostics.Process> instance needs to be disposed, which has a performance impact. | ||
- It's easy to forget to call <xref:System.Diagnostics.Process.Dispose> on the <xref:System.Diagnostics.Process> instance. | ||
- If nothing else besides `FileName` uses the `Process` instance, then the linked size grows unnecessarily by increasing the graph of types referenced. | ||
- It is somewhat difficult to discover or find this API. | ||
|
||
`System.Environment.ProcessPath` avoids all the above and produces the same information. | ||
|
||
> [!NOTE] | ||
> **System.Environment.ProcessPath** is available starting from .NET 6.0. | ||
## 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: | ||
|
||
```csharp | ||
using System.Diagnostics; | ||
|
||
class MyClass | ||
{ | ||
void MyMethod() | ||
{ | ||
string path = Process.GetCurrentProcess().MainModule.FileName; // Violation occurs | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Imports System.Diagnostics | ||
|
||
Class MyClass | ||
Private Sub MyMethod() | ||
Dim path As String = Process.GetCurrentProcess().MainModule.FileName | ||
End Function | ||
End Class | ||
``` | ||
|
||
```csharp | ||
using System.Diagnostics; | ||
|
||
class MyClass | ||
{ | ||
void MyMethod() | ||
{ | ||
string path = System.Environment.ProcessPath; // Violation fixed | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Imports System.Diagnostics | ||
|
||
Class MyClass | ||
Private Sub MyMethod() | ||
Dim path As String = System.Environment.ProcessPath | ||
End Function | ||
End Class | ||
``` | ||
|
||
> [!TIP] | ||
> A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press **Ctrl**+**.** (period). Choose **Use 'Environment.ProcessPath'** from the list of options that's presented. | ||
> | ||
> ![Code fix for CA1839 - Use 'Environment.ProcessPath'](media/ca1839-codefix.png) | ||
## When to suppress warnings | ||
|
||
It's safe to suppress a violation of this rule if you're not concerned about the performance impact from unnecessary allocation and eventual disposal of a <xref:System.Diagnostics.Process> and <xref:System.Diagnostics.ProcessModule> instances. | ||
|
||
## See also | ||
|
||
- [Performance rules](performance-warnings.md) | ||
- [Use 'Environment.ProcessId' instead of 'Process.GetCurrentProcess().Id'](ca1837.md) | ||
- [Use 'Environment.CurrentManagedThreadId' instead of 'Thread.CurrentThread.ManagedThreadId'](ca1840.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
--- | ||
title: "CA1840: Use 'Environment.CurrentManagedThreadId'" | ||
description: "'Environment.CurrentManagedThreadId' is simpler and faster than 'Thread.CurrentThread.ManagedThreadId'." | ||
ms.date: 11/23/2021 | ||
ms.topic: reference | ||
f1_keywords: | ||
- "CA1840" | ||
helpviewer_keywords: | ||
- "CA1840" | ||
author: buyaa-n | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
|
||
# CA1840: Use `Environment.CurrentManagedThreadId` instead of `Thread.CurrentThread.ManagedThreadId` | ||
|
||
| | Value | | ||
|-|-| | ||
| **Rule ID** |CA1840| | ||
| **Category** |[Performance](performance-warnings.md)| | ||
| **Fix is breaking or non-breaking** |Non-breaking| | ||
|
||
## Cause | ||
|
||
Using `Thread.CurrentThread.ManagedThreadId` for getting the current managed thread id instead of `Environment.CurrentManagedThreadId`. | ||
|
||
## Rule description | ||
|
||
`Environment.CurrentManagedThreadId` is more compact and efficient replacement of `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: | ||
|
||
```csharp | ||
using System.Threading; | ||
|
||
class MyClass | ||
{ | ||
void MyMethod() | ||
{ | ||
int id = Thread.CurrentThread.ManagedThreadId; // Violation occurs | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Imports System.Threading | ||
|
||
Class MyClass | ||
Private Sub MyMethod() | ||
Dim id As Integer = Thread.CurrentThread.ManagedThreadId | ||
End Function | ||
End Class | ||
``` | ||
|
||
```csharp | ||
using System.Threading; | ||
|
||
class MyClass | ||
{ | ||
void MyMethod() | ||
{ | ||
int id = System.Environment.CurrentManagedThreadId; // Violation fixed | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Imports System.Threading | ||
|
||
Class MyClass | ||
Private Sub MyMethod() | ||
Dim id As Integer = System.Environment.CurrentManagedThreadId | ||
End Function | ||
End Class | ||
``` | ||
|
||
> [!TIP] | ||
> A code fix is available for this rule in Visual Studio. To use it, position the cursor on the violation and press **Ctrl**+**.** (period). Choose **Use 'Environment.CurrentManagedThreadId'** from the list of options that's presented. | ||
> | ||
> ![Code fix for CA1840 - Use 'Environment.CurrentManagedThreadId'](media/ca1840-codefix.png) | ||
## When 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` | ||
|
||
## See also | ||
|
||
- [Performance rules](performance-warnings.md) | ||
- [Use 'Environment.ProcessId' instead of 'Process.GetCurrentProcess().Id'](ca1837.md) | ||
- [Use 'Environment.ProcessPath' instead of 'Process.GetCurrentProcess().MainModule.FileName'](ca1839.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters