CmdPal: Adding a lock around perf monitor updates (#46061)

Based on reported bug in Teams.

Added lock around OnLoadBasePage._loadCount and modified
PerformanceWidgetPage to use Interlocked.Increment/Decrement on
_loadCount.

---------

Co-authored-by: leileizhang <leilzh@microsoft.com>
Co-authored-by: Niels Laute <niels.laute@live.nl>
Co-authored-by: moooyo <42196638+moooyo@users.noreply.github.com>
Co-authored-by: Yu Leng <yuleng@microsoft.com>
Co-authored-by: Kai Tao <69313318+vanzue@users.noreply.github.com>
Co-authored-by: Jiří Polášek <me@jiripolasek.com>
Co-authored-by: Shawn Yuan <128874481+shuaiyuanxx@users.noreply.github.com>
Co-authored-by: Gordon Lam <73506701+yeelam-gordon@users.noreply.github.com>
Co-authored-by: Heiko <61519853+htcfreek@users.noreply.github.com>
Co-authored-by: Mike Hall <mikehall@microsoft.com>
Co-authored-by: vanzue <vanzue@outlook.com>
Co-authored-by: Jaylyn Barbee <51131738+Jaylyn-Barbee@users.noreply.github.com>
Co-authored-by: Thanh Nguyen <74597207+ThanhNguyxn@users.noreply.github.com>
Co-authored-by: Zach Teutsch <88554871+zateutsch@users.noreply.github.com>
This commit is contained in:
Michael Jolley
2026-03-11 14:06:44 -07:00
committed by GitHub
parent 39bbf0593e
commit b81ea23c68
6 changed files with 82 additions and 15 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Windows.Foundation;
@@ -70,6 +71,7 @@ internal abstract partial class OnLoadContentPage : OnLoadBasePage, IContentPage
internal abstract partial class OnLoadBasePage : Page
{
private readonly Lock _loadLock = new();
private int _loadCount;
#pragma warning disable CS0067 // The event is never used
@@ -82,22 +84,28 @@ internal abstract partial class OnLoadBasePage : Page
add
{
InternalItemsChanged += value;
if (_loadCount == 0)
lock (_loadLock)
{
Loaded();
}
if (_loadCount == 0)
{
Loaded();
}
_loadCount++;
_loadCount++;
}
}
remove
{
InternalItemsChanged -= value;
_loadCount--;
_loadCount = Math.Max(0, _loadCount);
if (_loadCount == 0)
lock (_loadLock)
{
Unloaded();
_loadCount--;
_loadCount = Math.Max(0, _loadCount);
if (_loadCount == 0)
{
Unloaded();
}
}
}
}