admin管理员组文章数量:1398984
I am creating an app for monitoring earbuds that are paired with my PC. The app is made using WPF and .Net 8.0. When the app is kept open without interaction (idle), it has a low CPU usage. However, when I create some object and then remove them after disposing, CPU count remains high.
I used the Task Manager
and the Windows Assessment and Deployment Kit (Windows ADK)
to monitor CPU usage. My PC has 7 paired devices and the CPU count for idle state in the second case is approximately 60,000. On the other hand, CPU count is significantly lower (around 200) for the first case.
Looks like the objects are still being monitored even after their disposal. Is there any way to keep the CPU count low in idle state?
My .cs file:
public partial class MainWindow : Window
{
public List<BluetoothDevice> pairedDevice = new List<BluetoothDevice>();
public List<string> IdList;
public MainWindow()
{
InitializeComponent();
IdList = new List<string>()
{
//IDs of my paired buds
};
}
private async void Start_Button_Click(object sender, RoutedEventArgs e)
{
StatusBorder.Background = new SolidColorBrush(Colors.Green);
for (int i = 0; i < IdList.Count; i++)
{
BluetoothDevice temp = await BluetoothDevice.FromIdAsync(IdList[i]);
temp.ConnectionStatusChanged += OnBudsConnectionStatusChanged;
pairedDevice.Add(temp);
Debug.WriteLine($"id: {IdList[i]}, {pairedDevice != null}");
}
}
private void OnBudsConnectionStatusChanged(BluetoothDevice sender, object args)
{
Debug.WriteLine($"Connection status changed to : {sender.ConnectionStatus}");
}
private async void Stop_Button_Click(object sender, RoutedEventArgs e)
{
StatusBorder.Background = new SolidColorBrush(Colors.Red);
for (int i = 0; i < IdList.Count; i++)
{
pairedDevice[i].ConnectionStatusChanged -= OnBudsConnectionStatusChanged;
pairedDevice[i].Dispose();
pairedDevice[i] = null;
}
pairedDevice.Clear();
}
}
And my Xaml file is:
<Window x:Class="BudsWatcherSample.MainWindow"
xmlns=";
xmlns:x=";
xmlns:d=";
xmlns:mc=";
xmlns:local="clr-namespace:BudsWatcherSample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel VerticalAlignment="Center">
<Border x:Name="StatusBorder" Margin="10" Width="20" Height="5" Background="Black"/>
<Button Content="Start" Click="Start_Button_Click" Width="50"/>
<Button Content="Stop" Click="Stop_Button_Click" Width="50"/>
</StackPanel>
</Window>
本文标签: bluetoothBluetoothDevice in WPF consumes too much CPUStack Overflow
版权声明:本文标题:bluetooth - BluetoothDevice in WPF consumes too much CPU - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744202254a2595020.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论