none
USB device removal hangs thread. VS 2015 - C#

    Question

  • I have an application which uses a USB interface. This device uses the standard Human Interface Device driver (HID). I run the device in a separate thread. If the device is removed during a read (quite likely). Then the thread hangs. Looking in detail at what it is doing it seems that it is stuck in function:

    bool UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, int reason, int pvLoopData)

    in "Application.cs", which is owned by Microsoft. What I think is happening is that this function is sitting there waiting for a message from the USB object. As the USB object is no longer present the message never arrives.

    If anyone has any bright ideas I would be most interested to hear them. I have tried various desperate measures like thread abort to no avail.

    Thanks...

    Wednesday, April 12, 2017 7:58 AM

All replies

  • Can you post code to look more.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Wednesday, April 12, 2017 8:57 AM
  • One of suggestions was to use BeginInvoke if you have some event handlers that react to device’s events and access the form controls: https://blogs.msdn.microsoft.com/dsui_team/2012/10/31/debugging-windows-forms-application-hangs-during-systemevents-userpreferencechanged/. Try simplifying the code in order to determine if this is the cause. The documentation for BeginInvoke contains a sample.


    • Edited by Viorel_MVP Wednesday, April 12, 2017 10:30 AM
    Wednesday, April 12, 2017 10:29 AM
  • Hi S W Quin,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about the development of C#. Since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum: Visual Studio Languages>Visual C# to seek for a more professional support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 13, 2017 8:52 AM