启动System.Windows.Forms.Timer的异常 [英] Exception Starting a System.Windows.Forms.Timer

查看:125
本文介绍了启动System.Windows.Forms.Timer的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个提供USB设备管理功能的应用程序.连接/删除设备后,Windows会通过我的应用程序的顶级Windows窗体的WinProc方法发送事件.

现在我已经观察到,对于单个连接,插入设备后,我得到一个connection,然后移除了另一个连接事件(该设备同时具有HID设备接口(我感兴趣的接口)和音频设备接口).现在我必须通过USB集线器一次连接很多这样的设备,并且连接的设备越多,这个up/down/up问题似乎越严重,似乎某些事件丢失了,这意味着该应用程序没有不能连接到所有设备.所以我的解决方案是,当我收到这些事件之一时,我启动了一个计时器(如果已经运行,则重新启动计时器).计时器的目的是与系统说的连接设备相比,检查我的应用程序认为连接的设备是否完好无损.因此,在收到上一个设备连接/删除事件后的第二分钟,每隔5秒我的应用程序就会检查连接的内容,而不是系统所说的已连接的内容.这使用System.Windows.Forms.Timer.在Tick事件处理期间,我调用计时器的Stop方法,然后在处理程序结尾处再次调用Start方法.问题是,当我在最后重新启动计时器时,有时会出现异常.

该异常发生在Windows.Forms.NativeWindow.CreateHandle(createParams cp)方法中,并显示消息错误创建窗口句柄".现在,我不处理计时器,只是继续使用它,根据需要调用stop和start.谁能解释这个例外?以及如何避免.

谢谢

Hi I have an application that provides management features for a USB device. When the device is connected/removed Windows sends an event via the WinProc method of the top level windows form of my application.

Now I have observed that for a single connection I get a connection , removal then another connection event when I plug in the device (the device has both an HID device interface (the one I am interested in ) and an audio device interface). Now I have to connect lots of these devices at once through a USB hub and this up/down/up problem seems to get worse the more devices you connect and it seems that some of the events go missing.This means that the application doesn''t connect to all the devices. So my solution was when I get one of these events I start a timer (or restart the timer if it is already running). The purpose of the timer is to sanity check the devices the my app considers to be connected compared to the device the system says is connected. So every 5 seconds for the next minute after I receive the last device connection/removal event my app checks what is connected compared to what the system says is connected. This uses a System.Windows.Forms.Timer. During the Tick event handling I call the Stop method of the timer and then at the end of the handler I call Start method again. The problem is that I sometimes get an exception when I restart the timer at the end.

The exception occurs in the Windows.Forms.NativeWindow.CreateHandle(createParams cp) method and has the message "Error creating window handle.". Now I don''t dispose of the timer I just keep re-using it calling stop and start as needed. Can anyone explain the exception ? and how to avoid it.

Thanks

推荐答案

我使用win32函数RegisterDeviceNotification传递设备广播接口(HID)和Forms窗口句柄来设置事件通知,然后使用以下内容来处理它

I use the win32 function RegisterDeviceNotification passing the device broadcast interface (HID) and the Forms window handle to set up the event notification then use the following to handle it

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    if (m.Msg == Win32Usb.WM_DEVICECHANGE)  
    {
        switch (m.WParam.ToInt32()) 
        {
            case Win32Usb.DEVICE_ARRIVAL:   // inserted
                _handler(true);
                break;
            case Win32Usb.DEVICE_REMOVECOMPLETE:    // removed
                _handler(false);
                break;
        }
    }
}



_handler将使用win32 interop调用来枚举已连接的HID设备,然后查找我们感兴趣的设备类型的PID和VID.我们保留一个已连接设备的列表,并检查是否每个发现的设备都在列表中.如果它在我们的列表中,我们将使用win32互操作调用来分配用于与其通信的设备文件.如果它在我们的列表中,但不在发现的设备列表中,则必须将其删除.为新设备分配资源后,我们便开始异步读取.此后不久,我们开始与设备进行握手类型的通信交换,以同步设备上的数据并获得有关设备的一些详细信息,例如序列号等.

现在,这对于某些设备来说都可以正常工作,但是我需要能够同时连接多达16个这些高功率设备.当使用USB集线器以树型形式将16个大功率设备全部连接并插入根集线器时,会尝试一次连接16个高功率设备.

基本上,当我连接这么多设备时,Windows似乎发疯了,设备似乎消失了,有时当我获得分配的资源时,读取失败或握手开始进行写入(因为已删除设备)时,读取失败.尘埃落定后,看起来所有设备都在系统上(通过设备管理器),但我们的应用程序尚未连接到所有设备,因此使用计时器进行了轮询.

我真的不能发布太多代码,因为这是一个商业项目.我基于 http://www.lvr.com/hidpage.htm [ ^ ]更具体地讲是buzz演示.



the _handler will use win32 interop calls to enumerate the connected HID devices then look for the PID and VID of the device type we are interested in. We keep a list of devices that we have already connected and check if each device discovered is on our list. If it on our list we use win32 interop calls to allocate the device file used to communicate with it. If it is in our list but not in the discovered device list it must have been removed. Once resources are allocated for a new device we then start an asynchronous read. A short time after that we start a Hand shake type communication exchange with the device to synchronise data on the device and get some details about the device such as serial numbers etc.

Now this all works fine for a few devices but I need to be able to connect upto 16 of these high power devices simultaneously. The problem occurs when 16 high power device try to connect at once when you connect them all in a tree type arrangement using usb hubs and plug in the root hub.

Basically when I connect this many device Windows seems to go nuts and the devices appear and disappear and sometimes when I get the resources allocated the read fails or perhaps when the handshake starts the write because the devices have been removed. When the dust settles it looks like all the device are there on the system(through device manager) but our application hasn''t connected to all of them hence the polling with the timer.

I can''t really post much code as this is a commercial project. I based all the USB classes and code on tutorials from http://www.lvr.com/hidpage.htm[^] more specifically the buzz demo.


这篇关于启动System.Windows.Forms.Timer的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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