I have a method that communicates with a HID device to retrieve a serial number, however the if the HID device is "present" but not active as is common when working with BT, the call for the serial will block and just sit there.
I would like to call my method but have it time out and return a empty serial if failed or the proper serial if successful.
Task.Run accomplishes most of this but does not kill the task if it hangs thus making my application impossible to exit.
var task = Task.Run(() => GetSerialStringWorker(device));
if (task.Wait(TimeSpan.FromMilliseconds(300)))
return task.Result;
else
{
//Need to be able to kill the task here!
return "00:00:00:00:00:00";
}
A cancellation token cannot be used either as the code that gets the serial is part of an external library and does not accept cancellation.
Also tried this, but it blocks on the thread.Join() and doesn't release properly when the thread is aborted;
string sn = "00:00:00:00:00:00";
System.Timers.Timer timeoutTimer = new System.Timers.Timer() { Interval = 300 };
Thread thread = new Thread(() =>
{
sn = GetSerialStringWorker(device);
timeoutTimer.Stop();
});
timeoutTimer.AutoReset = false;
timeoutTimer.Elapsed += (s, e) => { thread.Abort(); };
timeoutTimer.Start();
thread.Start();
thread.Join();
return sn;
Ok so for whatever reason the Join in the above example keeps my application from fully loading and just sits there blocking the rest of the apps initialization. Solution was to block it using a while command;
string sn = "";
Thread thread = new Thread(() =>
{
sn = GetSerialStringWorker(device);
});
var timeoutTimer = new System.Threading.Timer((s) =>
{
thread.Abort();
sn = "00:00:00:00:00:00";
}, null, 500, Timeout.Infinite);
thread.Start();
while (sn == "")
{
Thread.Sleep(1);
}
return sn;
This feels really ugly but for now it works until somebody can figure out a better way.