Attach event problem

Supporting Visual Studio on Windows
Post Reply
Cosmic76
Fresh meat
Posts: 1
Joined: Thu Aug 16, 2018 2:01 am
Contact:

Attach event problem

Post 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?
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Attach event problem

Post 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.
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Attach event problem

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests