UWP的HID库崩溃 [英] Crash of UWP's HID library

查看:97
本文介绍了UWP的HID库崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发可与HID设备一起使用的UWP应用程序.该应用程序几乎一直都在正常运行.但是当我关闭或折叠/恢复时,它通常会崩溃.我收到异常访问冲突读取位置:

I am developing a UWP application to work with HID devices. The application is working correctly almost all the time. But it often crashes when I close or collapse/restore. I got exception Access violation reading location:

Exception thrown at 0x5FC8A31C (Windows.Devices.HumanInterfaceDevice.dll) in MyApp.exe: 0xC0000005: Access violation reading location 0x0000001E.

并且此异常始终出现在CreateOutputReport方法上.

And this exception always appears on CreateOutputReport method.

如何检查创建输出报告的可能性?或者如何在UWP中处理此异常?

How can I check the possibility of the creation of the output report? Or how can I handle this exception in UWP?

非常感谢您提前提出建议.

Thanks for any advice in advance.

UPD:这是一个代码示例

UPD: Here is a code sample

HidDevice device = await HidDevice.FromIdAsync(id, FileAccessMode.ReadWrite);
if(device != null)
{
    var outReport = device.CreateOutputReport(); // Crashes here only on collapsing/closing
    byte[] buffer = new byte[(int)outReport.Data.Capacity];
    outReport.Data = buffer.AsBuffer();
    await device.SendOutputReportAsync(outReport);
}

UPD 2:从头开始测试.使用以下代码创建了UWP应用:

UPD 2: Tested from scratch. Created UWP app with this code:

public async void StartSending()
{
    var devices = await DeviceInformation.FindAllAsync(HidDevice.GetDeviceSelector(usagePage, usageId, vid, pid));

    if (devices.Any())
    {
        var device = devices.FirstOrDefault();

        HidDevice hidDevice = await HidDevice.FromIdAsync(device.Id, FileAccessMode.ReadWrite);

        if (hidDevice != null)
        {
            while(true)
            {
                var outReport = hidDevice.CreateOutputReport();

                byte[] buffer = new byte[(int)outReport.Data.Capacity];

                // fill data

                outReport.Data = buffer.AsBuffer();
                await hidDevice.SendOutputReportAsync(outReport);

                await Task.Delay(500);
            }
        }
    }
}

一切正常,直到我折叠应用程序为止.还原后应用程序失败.但这仅显示在已安装的应用程序上.在VS上可以正常使用.

Everything works great until I collapse the application. Application failed after restoring. But this appears only on the installed app. It works correctly from VS.

视频

推荐答案

感谢所有回复.@ XavierXie-MSFT是正确的,我没有任何暂停/继续的逻辑.现在可以使用了.

Thanks for all responses. @XavierXie-MSFT was right, I haven't any logic for suspending/resuming. It works now.

这是代码:

Application.Current.Suspending += OnSuspending;
Application.Current.Resuming += OnResuming;

private void OnResuming(object sender, object e)
{
    // Reinitialize all watchers for HID
    // Subscribe to all necessary events
}

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    // Remove all watchers for HID
    // Unsubscribe from all events
    // Dispose all active HID devices
}

这篇关于UWP的HID库崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆