static void Main(string[] args) { try { //Declare an frequencycounter object FrequencyCounter freq = new FrequencyCounter(); //Hook the basic event handlers freq.Attach += new AttachEventHandler(accel_Attach); freq.Detach += new DetachEventHandler(accel_Detach); freq.Error += new ErrorEventHandler(accel_Error); //hook the phidget specific event handlers freq.Count += new FrequencyCounterCountEventHandler(freq_Count); //open the acclerometer object for device connections freq.open(); //get the program to wait for an frequencycounter device to be attached Console.WriteLine("Waiting for frequencycounter to be attached...."); freq.waitForAttachment(); //enable the inputs for (int i = 0; i < freq.inputs.Count; i++) { freq.inputs[i].Enabled = true; freq.inputs[i].Filter = FrequencyCounterInput.FilterType.LOGIC_LEVEL; } //Get the program to wait for user input before moving on so that we can //watch for some events Console.WriteLine("Press any key to end"); Console.Read(); //If user input has been read, we can now terminate the program, so //close the phidget object freq.close(); //set the object to null to clear it from memory freq = null; //if no exceptions have been trhown at this point, the program can //terminate safely Console.WriteLine("ok"); } catch (PhidgetException ex) { Console.WriteLine(ex.Description); } }
//When the form is being close, make sure to stop all the motors and close the Phidget. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { freqCounter.Attach -= new AttachEventHandler(freqCounter_Attach); freqCounter.Detach -= new DetachEventHandler(freqCounter_Detach); freqCounter.Error -= new ErrorEventHandler(freqCounter_Error); freqCounter.Count -= new FrequencyCounterCountEventHandler(freqCounter_Count); Application.DoEvents(); //run any events in the message queue - otherwise close will hang if there are any outstanding events Application.DoEvents(); freqCounter.close(); freqCounter = null; }