Page 1 of 1

Attach event problem

Posted: Thu Aug 16, 2018 2:33 am
by Cosmic76
Hi,
I experienced some problem using the Attach Event Handler on my c# app.
When I defined the Attach Event subscription, the callback event method 'onAttachedHandler' work correctly but the channel ch0 don't want to open, a Phidget timeout exception is raised:
Phidget22.PhidgetException: PhidgetException 0x00000003 ( Timed Out)

If I remove the Attach Event subscription, the channel ch0 start to open correctly.

Code: Select all

using Phidget22;
using Phidget22.Events;

namespace HubTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            VoltageRatioInput ch0 = new VoltageRatioInput();


            ch0.Channel = 0;
            ch0.Attach += onAttachHandler;  //without subscription ch0 will open fine

            try
            {
               
                ch0.Open(500);
                
                status.Text = ch0.ChannelClass.ToString();
            }

            catch (PhidgetException ex)
            {
                status.Text = "Stato:" + ex;
            }
        }

         void onAttachHandler(object sender, Phidget22.Events.AttachEventArgs e)
            {
            status.ForeColor = System.Drawing.Color.Green;
            }
        
    }
}
Someone could take a look?

Re: Attach event problem

Posted: Thu Aug 16, 2018 11:57 am
by mparadis
Hmm, it looks like this might be an issue with our libraries. I think you'll find that ch0 is attaching, and that you can use it after the try/catch in your program, but for some reason a timeout exception is being thrown. If you remove the timeout duration from open, you won't get the exception and the device will attach normally. I'll notify our developer about this issue.

Re: Attach event problem

Posted: Mon Aug 20, 2018 1:36 pm
by Patrick
The issue is using both the attach event and the open with timeout from the form load event - this causes a deadlock because the phidget attach event has been queued to fire after the form load event returns (on the GUI thread), but open(timeout) will not return until after the attach event returns.

Generally, for GUI apps, use the attach event and open(), and move the post-open initialization code into the attach handler, for simple command line programs, you can use open(timeout), and forgo the attach event.

-Patrick