//When the application is being terminated, close the Phidget. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //Unhook the event handlers encoder.Attach -= new AttachEventHandler(encoder_Attach); encoder.Detach -= new DetachEventHandler(encoder_Detach); encoder.Error -= new ErrorEventHandler(encoder_Error); encoder.PositionChange -= new EncoderPositionChangeEventHandler(encoder_PositionChange); encoder.InputChange -= new InputChangeEventHandler(encoder_InputChange); //run any events in the message queue - otherwise close will hang if there are any outstanding events Application.DoEvents(); encoder.close(); }
static void Main(string[] args) { try { //Initialize the Encoder object encoder = new Phidgets.Encoder(); //Hook the basic event handlers encoder.Attach += new AttachEventHandler(encoder_Attach); encoder.Detach += new DetachEventHandler(encoder_Detach); encoder.Error += new ErrorEventHandler(encoder_Error); //Hook the phidget specific event handlers encoder.InputChange += new InputChangeEventHandler(encoder_InputChange); encoder.PositionChange += new EncoderPositionChangeEventHandler (encoder_PositionChange); //open the object for connections encoder.open(); //Wait for an encoder device to be attached before continuing Console.WriteLine("Waiting for Encoder to be attached...."); encoder.waitForAttachment(); //Wait for user input before continuing so that we can see the events //being sent when using the encoder Console.WriteLine("Press any key to end..."); Console.Read(); //Since user input is read we can terminate the program, so we'll close //the encoder object encoder.close(); //set the object to null to get it out of memory encoder = null; //if no expcetions where thrown at this point it is safe to terminate Console.WriteLine("ok"); } catch (PhidgetException ex) { Console.WriteLine(ex.Description); } }