<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[James's Programming Page — HidSharp: Occasional ArgumentException thrown by HidManager.GetDevices]]></title>
	<link rel="self" href="https://swforum.seekye.com/feed/atom/topic/10072/" />
	<updated>2014-03-07T00:15:16Z</updated>
	<generator>PunBB</generator>
	<id>https://swforum.seekye.com/topic/10072/</id>
		<entry>
			<title type="html"><![CDATA[HidSharp: Occasional ArgumentException thrown by HidManager.GetDevices]]></title>
			<link rel="alternate" href="https://swforum.seekye.com/post/12923/#p12923" />
			<content type="html"><![CDATA[<p>Hi,</p><p>My details:<br /></p><ul><li><p>Windows 7 64-bit</p></li><li><p>HidSharp: 1.5</p></li><li><p>.NET Framework 4 Client Profile</p></li><li><p>Building a 32-bit WinForms program</p></li></ul><p>I&#039;m hitting an intermittent ArgumentException in HidManager.GetDevices. The following line in that method:</p><div class="codebox"><pre><code>lock (_deviceList) { _deviceList.Add(addition_, device); }</code></pre></div><p>occasionally throws an ArgumentException saying that the key already exists. My set up is a WinForms program that has the following method in the form class:</p><div class="codebox"><pre><code>    private const int WM_DEVICECHANGE = 0x0219;
    private const int DBT_DEVNODES_CHANGED = 0x0007;
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == MainForm.WM_DEVICECHANGE &amp;&amp; m.WParam.ToInt32() == DBT_DEVNODES_CHANGED)
        {
            HidDeviceLoader loader = new HidDeviceLoader();
            foreach (HidDevice device in loader.GetDevices())
            {
                // Do something
            }
        }
    }</code></pre></div><p>This code can be triggered multiple times in rapid succession which might be relevant. If I run my program and repeatedly plug and unplug a USB device eventually the ArgumentException occurs.</p><p>I added some Console.WriteLines to HidManager.GetDevices which produced the following output. It appears that something else is modifying _deviceList between the start of GetDevices and the point at which _deviceList.Add is called. Maybe there are threads still running from a previous call to GetDevices, I&#039;m not sure:</p><div class="codebox"><pre><code>    _deviceList.Keys (start of GetDevices):
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col01#7&amp;1d798885&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_01&amp;col01#7&amp;1f93e0c0&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_051d&amp;pid_0002#6&amp;145c4476&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_01&amp;col02#7&amp;1f93e0c0&amp;0&amp;0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_00#7&amp;7d0547b&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col02#7&amp;1d798885&amp;0&amp;0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col03#7&amp;1d798885&amp;0&amp;0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col04#7&amp;1d798885&amp;0&amp;0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_01#7&amp;30a9a4c3&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    devices (after catching the ArgumentException):
    \\?\hid#vid_045e&amp;pid_028e&amp;ig_00#7&amp;1f6318a&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_00#7&amp;7d0547b&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_01&amp;col01#7&amp;1f93e0c0&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_01&amp;col02#7&amp;1f93e0c0&amp;0&amp;0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_051d&amp;pid_0002#6&amp;145c4476&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col01#7&amp;1d798885&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col02#7&amp;1d798885&amp;0&amp;0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col03#7&amp;1d798885&amp;0&amp;0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col04#7&amp;1d798885&amp;0&amp;0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_01#7&amp;30a9a4c3&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    additions (after catching the ArgumentException):
    \\?\hid#vid_045e&amp;pid_028e&amp;ig_00#7&amp;1f6318a&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    removals (after catching the ArgumentException):
    _deviceList.Keys:
    \\?\hid#vid_045e&amp;pid_028e&amp;ig_00#7&amp;1f6318a&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col01#7&amp;1d798885&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_01&amp;col01#7&amp;1f93e0c0&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_051d&amp;pid_0002#6&amp;145c4476&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_01&amp;col02#7&amp;1f93e0c0&amp;0&amp;0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_04d9&amp;pid_1818&amp;mi_00#7&amp;7d0547b&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col02#7&amp;1d798885&amp;0&amp;0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col03#7&amp;1d798885&amp;0&amp;0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_00&amp;col04#7&amp;1d798885&amp;0&amp;0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
    \\?\hid#vid_1e7d&amp;pid_2e22&amp;mi_01#7&amp;30a9a4c3&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
    addition_ (after catching the ArgumentException):
    \\?\hid#vid_045e&amp;pid_028e&amp;ig_00#7&amp;1f6318a&amp;0&amp;0000#{4d1e55b2-f16f-11cf-88cb-001111000030}</code></pre></div><p>I&#039;ve worked around this problem in my code now so it&#039;s not an issue for me but I thought I would report the problem anyway. To Zer: thank you for HidSharp, other than this one issue it has been absolutely perfect, just what I was looking for.</p>]]></content>
			<author>
				<name><![CDATA[Pike]]></name>
				<uri>https://swforum.seekye.com/user/14160/</uri>
			</author>
			<updated>2014-03-07T00:15:16Z</updated>
			<id>https://swforum.seekye.com/post/12923/#p12923</id>
		</entry>
</feed>
