dotnet / dotnet/winforms

Control.CheckForIllegalCrossThreadCalls to use Interlocked.Increment/Decrement

Open
#13,172 6 comments 0 reactions 1 assignee Claimed by @JeremyKuhne View on GitHub
api-suggestion
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### Background and motivation

Control.CheckForIllegalCrossThreadCalls is Boolean value that is not thread safe.

### API Proposal

Replace with

```csharp
Control.BeginIllegalCrossThreadCall() => Interlocked.Increment(...);
Control.EndIllegalCrossThreadCall() => Interlocked.Decrement(...);
Control.CheckForIllegalCrossThreadCalls => (Interlocked.Read(...) == 0);

### API Usage

```csharp

Control.BeginIllegalCrossThreadCall() ;
try
{
// Get control handle, etc.
nint handle = control.Handle;

}
finally
{
Control.EndIllegalCrossThreadCall() ;
}

```

### Alternative Designs

Control.CheckForIllegalCrossThreadCalls could be [ ThreadStatic ].

### Risks

_No response_

### Will this feature affect UI controls?

Yes.

Will VS Designer need to support the feature? No
What impact will it have on accessibility? No
Will this feature need to be localized or be localizable? No

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.