New Control API for locking/unlocking drawing
- Dominant language
- C#
- Stars
- 4.9k
- Forks
- 1.1k
- Avg merge
- 1d 13m
- Merged PRs (30d)
- 85
Description
### Background and motivation
In WinForms development, there are scenarios where extensive updates to a control's state can cause flickering or performance degradation. For example, adding a large number of items to a list or making bulk changes to a grid may lead to unnecessary redraws. Developers often handle this by temporarily disabling the control's redrawing, applying changes, and re-enabling drawing afterward.
Currently, there is no built-in, intuitive mechanism in .NET WinForms to lock and unlock drawing for controls. This API proposal introduces methods to facilitate this, reducing flicker, improving performance, and providing a clean, structured way to manage drawing locks using a disposable pattern in C#.
### API Proposal
```csharp
public static void LockDrawing(this Control target);
public static void UnlockDrawing(this Control target);
public static DrawingLock UseDrawingLock(this Control target);
public ref struct DrawingLock : IDisposable
{
public DrawingLock(Control target);
public void Dispose();
}
```
### API Usage
```csharp
var control = someControl;
control.LockDrawing();
// Perform bulk updates
control.UnlockDrawing();
var control = someControl;
using (control.UseDrawingLock())
{
// Perform bulk updates
}
```
### Alternative Designs
_No response_
### Risks
1. Error Handling - If UnlockDrawing is not called (e.g., due to an exception), the control may remain in a non-repainting state.
2. Thread Safety - Accessing the Control.Handle from a thread other than the UI thread may cause runtime errors.
3. Invalid Handles - Calling these methods on controls without a valid handle (e.g., before the control is created or after it is disposed) may fail.
### Will this feature affect UI controls?
n/a
Contributor guide
Assessment
This issue has not been assessed yet.