Changed locks to use the .NET 9 Lock-Type if possible

This commit is contained in:
DarthAffe 2025-06-10 23:06:34 +02:00
parent af9f75c63e
commit d0388f8c75
12 changed files with 63 additions and 27 deletions

View File

@ -35,7 +35,8 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
#region Properties & Fields #region Properties & Fields
private readonly object _captureLock = new(); private readonly Lock _captureLock = new();
private readonly Lock _texturesLock = new();
private readonly bool _useNewDuplicationAdapter; private readonly bool _useNewDuplicationAdapter;
@ -140,7 +141,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{ {
if (_context == null) return; if (_context == null) return;
lock (_textures) lock (_texturesLock)
{ {
if (!_textures.TryGetValue(captureZone, out ZoneTextures? textures)) return; if (!_textures.TryGetValue(captureZone, out ZoneTextures? textures)) return;
@ -258,7 +259,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{ {
CaptureZone<ColorBGRA> captureZone = base.RegisterCaptureZone(x, y, width, height, downscaleLevel); CaptureZone<ColorBGRA> captureZone = base.RegisterCaptureZone(x, y, width, height, downscaleLevel);
lock (_textures) lock (_texturesLock)
InitializeCaptureZone(captureZone); InitializeCaptureZone(captureZone);
return captureZone; return captureZone;
@ -269,7 +270,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{ {
if (!base.UnregisterCaptureZone(captureZone)) return false; if (!base.UnregisterCaptureZone(captureZone)) return false;
lock (_textures) lock (_texturesLock)
{ {
if (_textures.TryGetValue(captureZone, out ZoneTextures? textures)) if (_textures.TryGetValue(captureZone, out ZoneTextures? textures))
{ {
@ -291,7 +292,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
//TODO DarthAffe 01.05.2022: For now just reinitialize the zone in that case, but this could be optimized to only recreate the textures needed. //TODO DarthAffe 01.05.2022: For now just reinitialize the zone in that case, but this could be optimized to only recreate the textures needed.
if ((width != null) || (height != null) || (downscaleLevel != null)) if ((width != null) || (height != null) || (downscaleLevel != null))
{ {
lock (_textures) lock (_texturesLock)
{ {
if (_textures.TryGetValue(captureZone, out ZoneTextures? textures)) if (_textures.TryGetValue(captureZone, out ZoneTextures? textures))
{ {
@ -383,7 +384,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
base.Restart(); base.Restart();
lock (_captureLock) lock (_captureLock)
lock (_textures) lock (_texturesLock)
{ {
try try
{ {

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks> <TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
@ -54,6 +54,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> <DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Resources\icon.png"> <None Include="Resources\icon.png">
<Pack>True</Pack> <Pack>True</Pack>

View File

@ -17,7 +17,7 @@ public sealed class DX9ScreenCapture : AbstractScreenCapture<ColorBGRA>
{ {
#region Properties & Fields #region Properties & Fields
private readonly object _captureLock = new(); private readonly Lock _captureLock = new();
private readonly IDirect3D9 _direct3D9; private readonly IDirect3D9 _direct3D9;
private IDirect3DDevice9? _device; private IDirect3DDevice9? _device;

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks> <TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
@ -54,6 +54,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> <DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Resources\icon.png"> <None Include="Resources\icon.png">
<Pack>True</Pack> <Pack>True</Pack>

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks> <TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier> <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
@ -54,6 +54,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> <DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Resources\icon.png"> <None Include="Resources\icon.png">
<Pack>True</Pack> <Pack>True</Pack>

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
using HPPH; using HPPH;
namespace ScreenCapture.NET; namespace ScreenCapture.NET;
@ -14,7 +15,7 @@ public sealed class X11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{ {
#region Properties & Fields #region Properties & Fields
private readonly object _captureLock = new(); private readonly Lock _captureLock = new();
private nint _display; private nint _display;
private nint _drawable; private nint _drawable;
@ -62,12 +63,11 @@ public sealed class X11ScreenCapture : AbstractScreenCapture<ColorBGRA>
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer) protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
{ {
using IDisposable @lock = captureZone.Lock(); using IDisposable @lock = captureZone.Lock();
{
if (captureZone.DownscaleLevel == 0) if (captureZone.DownscaleLevel == 0)
CopyZone(captureZone, buffer); CopyZone(captureZone, buffer);
else else
DownscaleZone(captureZone, buffer); DownscaleZone(captureZone, buffer);
}
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -0,0 +1,8 @@
#if NET8_0
// ReSharper disable once CheckNamespace
namespace ScreenCapture.NET.Compatibility.Net8;
public sealed class Lock;
#endif

View File

@ -119,7 +119,6 @@ public static class BlackBarDetection
#endregion #endregion
#region Image #region Image
/// <summary> /// <summary>

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using HPPH; using HPPH;
namespace ScreenCapture.NET; namespace ScreenCapture.NET;
@ -13,10 +14,12 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
private bool _isDisposed; private bool _isDisposed;
protected readonly Lock CaptureZonesLock = new();
/// <summary> /// <summary>
/// Gets a list of <see cref="CaptureZone{TColol}"/> registered on this ScreenCapture. /// Gets a list of <see cref="CaptureZone{TColol}"/> registered on this ScreenCapture.
/// </summary> /// </summary>
protected HashSet<CaptureZone<TColor>> CaptureZones { get; } = new(); protected HashSet<CaptureZone<TColor>> CaptureZones { get; } = [];
/// <inheritdoc /> /// <inheritdoc />
public Display Display { get; } public Display Display { get; }
@ -63,7 +66,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
result = false; result = false;
} }
lock (CaptureZones) lock (CaptureZonesLock)
foreach (CaptureZone<TColor> captureZone in CaptureZones.Where(x => x.AutoUpdate || x.IsUpdateRequested)) foreach (CaptureZone<TColor> captureZone in CaptureZones.Where(x => x.AutoUpdate || x.IsUpdateRequested))
{ {
try try
@ -113,7 +116,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
{ {
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName); if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
lock (CaptureZones) lock (CaptureZonesLock)
{ {
ValidateCaptureZoneAndThrow(x, y, width, height, downscaleLevel); ValidateCaptureZoneAndThrow(x, y, width, height, downscaleLevel);
@ -195,7 +198,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
{ {
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName); if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
lock (CaptureZones) lock (CaptureZonesLock)
{ {
if (!CaptureZones.Contains(captureZone)) if (!CaptureZones.Contains(captureZone))
throw new ArgumentException("The capture zone is not registered to this ScreenCapture", nameof(captureZone)); throw new ArgumentException("The capture zone is not registered to this ScreenCapture", nameof(captureZone));

View File

@ -16,7 +16,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
{ {
#region Properties & Fields #region Properties & Fields
private readonly object _lock = new(); private readonly Lock _lock = new();
/// <inheritdoc /> /// <inheritdoc />
public Display Display { get; } public Display Display { get; }
@ -150,7 +150,11 @@ public sealed class CaptureZone<TColor> : ICaptureZone
/// <inheritdoc /> /// <inheritdoc />
public IDisposable Lock() public IDisposable Lock()
{ {
#if NET8_0
Monitor.Enter(_lock); Monitor.Enter(_lock);
#else
_lock.Enter();
#endif
return new UnlockDisposable(_lock); return new UnlockDisposable(_lock);
} }
@ -187,14 +191,14 @@ public sealed class CaptureZone<TColor> : ICaptureZone
#region Properties & Fields #region Properties & Fields
private bool _disposed = false; private bool _disposed = false;
private readonly object _lock; private readonly Lock _lock;
#endregion #endregion
#region Constructors #region Constructors
// ReSharper disable once ConvertToPrimaryConstructor // ReSharper disable once ConvertToPrimaryConstructor
public UnlockDisposable(object @lock) => this._lock = @lock; public UnlockDisposable(Lock @lock) => this._lock = @lock;
~UnlockDisposable() => Dispose(); ~UnlockDisposable() => Dispose();
#endregion #endregion
@ -206,7 +210,12 @@ public sealed class CaptureZone<TColor> : ICaptureZone
{ {
ObjectDisposedException.ThrowIf(_disposed, this); ObjectDisposedException.ThrowIf(_disposed, this);
#if NET8_0
Monitor.Exit(_lock); Monitor.Exit(_lock);
#else
_lock.Exit();
#endif
_disposed = true; _disposed = true;
GC.SuppressFinalize(this); GC.SuppressFinalize(this);

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks> <TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@ -53,6 +53,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> <DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Resources\icon.png"> <None Include="Resources\icon.png">
<Pack>True</Pack> <Pack>True</Pack>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks> <TargetFrameworks>net9.0</TargetFrameworks>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>