Connecting Tech Pros Worldwide Forums | Help | Site Map

Closing Blocked Thread

Newbie
 
Join Date: Aug 2008
Location: Hawaii
Posts: 9
#1: 2 Weeks Ago
Hi everyone,

I am working with a library (HIDLibrary) to read from and hid device (eHome Infraread Transceiver, aka Windows Media Center Remote) to read from a universal remote control. The library functions perfectly, even with other devices. However, my problem lies in the fact that I am running my polling function in another thread, as the library's read function blocks the thread until input is received. While my app is running, this is fine, but of course not when trying to close my app. Calling the library's CloseDevice() function does not cancel the read either. Would any of you kind people happen to know the correct way to kill a thread blocked during an IO call? I've heard of P/Invoke to TerminateThread but also read that is not recommended. Any insight would be helpful. Thank you.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,779
#2: 2 Weeks Ago

re: Closing Blocked Thread


Does thread.abort() work?
Newbie
 
Join Date: Aug 2008
Location: Hawaii
Posts: 9
#3: 2 Weeks Ago

re: Closing Blocked Thread


Thread.Abort() does not work, as the library is waiting for input from the usb device before finishing the threadstart. If I try to close the app, then press a button on the remote, it works fine. However this is not exactly preferred. I even tried setting the thread to background, but no joy.
Newbie
 
Join Date: Aug 2008
Location: Hawaii
Posts: 9
#4: 2 Weeks Ago

re: Closing Blocked Thread


Well I've found a solution that is at least more satisfactory. I rewrote my Reader class to be intstance based rather than a static class, and instead of running in a separate looped thread, I do an async read on the device, then close/open the device again, and run the read function again from my callback:

private void Listen()
{
device.Read(new HidDevice.ReadCallback(OnDataReceived));
}
private void OnDataReceived(HidDeviceData hid_data)
{
if (this.DataReceived != null){this.DataReceived(this, new HIDDataEventArgs(hid_data));}
device.CloseDevice();
device.OpenDevice();
Listen();
}
This works for my form-based app to edit different remote profiles and assign actions to buttons, but will not do for my service to convert the hid data received to keystrokes using keyb_event. Any ideas? I'll gladly give my code to anyone interested, as this is a personal project, not commercial...
Reply