Skip to content

Commit 6fc5d03

Browse files
committed
Fix Lock misuses
Note to myself: Please read Microsoft documentation more comprehensively before using a feature :derp:
1 parent d105eaa commit 6fc5d03

File tree

8 files changed

+17
-17
lines changed

8 files changed

+17
-17
lines changed

CollapseLauncher/Classes/Helper/Background/Loaders/MediaPlayerLoader.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ Playing background video with FFmpeg!
235235

236236
private void UpdateCanvasOnSizeChangeEvent(object sender, SizeChangedEventArgs e)
237237
{
238-
lock (_currentLock)
238+
using (_currentLock.EnterScope())
239239
{
240240
float scalingFactor = (float)WindowUtility.CurrentWindowMonitorScaleFactor;
241241
float newWidth = (float)(e.NewSize.Width * scalingFactor);
@@ -301,7 +301,7 @@ public void DisposeMediaModules()
301301

302302
if (IsUseVideoBgDynamicColorUpdate)
303303
{
304-
lock (_currentLock)
304+
using (_currentLock.EnterScope())
305305
{
306306
_currentCanvasDrawingSession?.Dispose();
307307
Interlocked.Exchange(ref _currentCanvasDrawingSession, null);
@@ -373,7 +373,7 @@ private static async Task<StorageFile> GetFileAsStorageFile(string filePath)
373373
private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
374374
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
375375
{
376-
lock (_currentLock)
376+
using (_currentLock.EnterScope())
377377
{
378378
if (_currentCanvasVirtualImageSource is null)
379379
{
@@ -391,7 +391,7 @@ private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
391391

392392
try
393393
{
394-
lock (_currentLock)
394+
using (_currentLock.EnterScope())
395395
{
396396
mediaPlayer.CopyFrameToVideoSurface(_currentCanvasBitmap);
397397
_currentCanvasDrawingSession = _currentCanvasVirtualImageSource
@@ -413,7 +413,7 @@ private async void FrameGrabberEvent(MediaPlayer mediaPlayer, object args)
413413
#endif
414414
finally
415415
{
416-
lock (_currentLock)
416+
using (_currentLock.EnterScope())
417417
{
418418
CurrentDispatcherQueue.TryEnqueue(() =>
419419
{

CollapseLauncher/Classes/Helper/Hash.StringAndBytes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public static bool TryGetHashFromBytes<T>(
117117
where T : NonCryptographicHashAlgorithm
118118
{
119119
// Lock the thread and append the span to the hash algorithm
120-
lock (hashSource.Lock)
120+
using (hashSource.Lock.EnterScope())
121121
{
122122
// Append and calculate the hash of the span
123123
hashSource.Hash.Append(source);
@@ -235,7 +235,7 @@ public static bool TryGetCryptoHashFromBytes<T>(
235235
where T : HashAlgorithm
236236
{
237237
// Lock the thread and compute the hash of the span
238-
lock (hashSource.Lock)
238+
using (hashSource.Lock.EnterScope())
239239
{
240240
// Reset the hash instance state.
241241
hashSource.Hash.Initialize();

CollapseLauncher/Classes/Helper/HttpClientBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public HttpClientBuilder<THandler> UseLauncherConfig(int maxConnections = MaxCon
112112
UseExternalProxy(lProxyUri, lHttpProxyUsername, proxyPassword);
113113
}
114114

115-
lock (HttpClientBuilderSharedLock)
115+
using (HttpClientBuilderSharedLock.EnterScope())
116116
{
117117
if (!skipDnsInit && lIsUseExternalDns && ExternalDnsServers == null)
118118
{

CollapseLauncher/Classes/Helper/Metadata/DataCooker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ internal static void ServeV3Data(ReadOnlySpan<byte> data, Span<byte>
108108
: new byte[dataRawBuffer.Length];
109109
isDecryptPoolUsed = isDecryptPoolAllowed;
110110

111-
lock (RsaDecryptLock)
111+
using (RsaDecryptLock.EnterScope())
112112
{
113113
if (RsaInstance == null)
114114
{

CollapseLauncher/Classes/InstallManagement/Base/InstallManagerBase.SophonPatch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ async ValueTask ImplDownload(Tuple<SophonPatchAsset, Dictionary<string, int>> ct
318318
SophonPatchAsset patchAsset = ctx.Item1;
319319
Dictionary<string, int> downloadedDict = ctx.Item2;
320320

321-
lock (dictionaryLock)
321+
using (dictionaryLock.EnterScope())
322322
{
323323
_ = downloadedDict.TryAdd(patchAsset.PatchNameSource, 0);
324324
downloadedDict[patchAsset.PatchNameSource]++;
@@ -347,7 +347,7 @@ async ValueTask ImplPatchUpdate(Tuple<SophonPatchAsset, Dictionary<string, int>>
347347
SophonPatchAsset patchAsset = ctx.Item1;
348348
Dictionary<string, int> downloadedDict = ctx.Item2;
349349

350-
lock (dictionaryLock)
350+
using (dictionaryLock.EnterScope())
351351
{
352352
if (!string.IsNullOrEmpty(patchAsset.PatchNameSource) &&
353353
downloadedDict.Remove(patchAsset.PatchNameSource))

CollapseLauncher/Classes/RegistryMonitor/RegistryMonitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public RegChangeNotifyFilter RegChangeNotifyType
164164
get { return _regFilter; }
165165
set
166166
{
167-
lock (_threadLock)
167+
using (_threadLock.EnterScope())
168168
{
169169
if (IsMonitoring)
170170
throw new InvalidOperationException("Monitoring thread is already running");
@@ -247,7 +247,7 @@ public void Start()
247247
if (_disposed)
248248
throw new ObjectDisposedException(null, "This instance is already disposed");
249249

250-
lock (_threadLock)
250+
using (_threadLock.EnterScope())
251251
{
252252
if (IsMonitoring)
253253
{
@@ -270,7 +270,7 @@ public void Stop()
270270
{
271271
if (_disposed) return;
272272

273-
lock (_threadLock)
273+
using (_threadLock.EnterScope())
274274
{
275275
Thread thread = _thread;
276276
if (thread == null)

CollapseLauncher/XAMLs/MainApp/Pages/FileCleanupPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ await Task.Factory.StartNew(state =>
386386
catch (Exception ex)
387387
{
388388
Interlocked.Increment(ref deleteFailed);
389-
lock (failedListLock)
389+
using (failedListLock.EnterScope())
390390
{
391391
failedList.Add(fileInfo);
392392
}

Hi3Helper.Core/Classes/Logger/LoggerBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void SetFolderPathAndInitialize(string folderPath, Encoding logEncoding)
5151
}
5252
#endif
5353

54-
lock (_lockObject)
54+
using (_lockObject.EnterScope())
5555
{
5656
// Try dispose the _logWriter even though it's not initialized.
5757
// This will be used if the program need to change the log folder to another location.
@@ -76,7 +76,7 @@ public void SetFolderPathAndInitialize(string folderPath, Encoding logEncoding)
7676
#nullable enable
7777
public void ResetLogFiles(string? reloadToPath, Encoding? encoding = null)
7878
{
79-
lock (_lockObject)
79+
using (_lockObject.EnterScope())
8080
{
8181
DisposeBase();
8282

0 commit comments

Comments
 (0)