Non-Blocking Input Events

Supporting Visual Studio on Windows
Post Reply
scsm
Phidgetly
Posts: 24
Joined: Sun Nov 20, 2022 10:28 pm
Contact:

Non-Blocking Input Events

Post by scsm »

I receive events as digital input from an IR device. I want to perform a bunch of things that will run async and take several seconds when the state of the input is true. During this time I will ignore all other state changes on this input.

Code: Select all

private async void DigitalInput_StateChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
{
   try
   {
      if (isInitialised)
      {
         await DigitalInputStatus((DigitalInput)sender, e.State);
      }
    }
   catch (PhidgetException ex)
   {
      Debug.WriteLine("[ERROR] StateChanged.PhEx " + ex.ErrorCode + " (" + ex.Description + "): " + ex.Detail);
   }
}
How can I make this non-blocking on the phidget? I want to immediately return and not "hold" onto the thread that called this event?

Is there any coding pattern or example for this scenario. i.e. running long running tasks from a phidget event?
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Non-Blocking Input Events

Post by Patrick »

If you're interacting with a UI I'd use a BackgroundWorker, otherwise use a ThreadPool or Thread.

If you don't care about events at all until your task is finished, you can just deregister/register for the StateChange event at the start/end of your task.

-Patrick
scsm
Phidgetly
Posts: 24
Joined: Sun Nov 20, 2022 10:28 pm
Contact:

Re: Non-Blocking Input Events

Post by scsm »

We haven't tested it under load yet but it seems to work with a ThreadPool. Thanks for the suggestion.

Code: Select all

if (!scales.isWeighing)
{
    ThreadPool.QueueUserWorkItem(a => BeginWeighing(scales));
}
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 0 guests