//When the application is terminating, close the Phidget. private void Form1_FormClosed(object sender, FormClosedEventArgs e) { ph_sensor.Attach -= new AttachEventHandler(ph_sensor_Attach); ph_sensor.Detach -= new DetachEventHandler(ph_sensor_Detach); ph_sensor.Error -= new ErrorEventHandler(ph_sensor_Error); ph_sensor.PHChange -= new PHChangeEventHandler(ph_sensor_PHChange); //run any events in the message queue - otherwise close will hang if there are any outstanding events Application.DoEvents(); ph_sensor.close(); ph_sensor = null; }
static void Main(string[] args) { try { PHSensor ph_sensor = new PHSensor(); //Declare a PHSensor object //hook the basic event handlers ph_sensor.Attach += new AttachEventHandler(ph_sensor_Attach); ph_sensor.Detach += new DetachEventHandler(ph_sensor_Detach); ph_sensor.Error += new ErrorEventHandler(ph_sensor_Error); //hook the phidget specific event handlers ph_sensor.PHChange += new PHChangeEventHandler(ph_sensor_PHChange); //open the phidget object to allow connections with phidget PH sensor //devices ph_sensor.open(); //Make the program wait for a Phidget PHSensor to be attached before //trying to communicate with one If we try and call the methods of the //ph_sensor object without a PHSensor device attached, a PhidgetException //will be thrown Console.WriteLine("Waiting for PHSensor to be attached...."); ph_sensor.waitForAttachment(); //Get the program to wait for user input to continue so that we can see //some events fire and console output to be displayed Console.WriteLine("Hit any key to end the program...."); Console.Read(); //Since user input was read, we'll terminate the program, so close the //ph_sensor object ph_sensor.close(); //null he object to clear it from memory ph_sensor = null; //If no exceptions were thrown at this point, the program is ok to //terminate Console.WriteLine("ok"); } catch (PhidgetException ex) { Console.WriteLine(ex.Description); } }