Safely Making and hanging up calls

Jun 24, 2010 at 2:46 PM
Edited Jun 24, 2010 at 2:47 PM

I'm having issues hanging up calls.

Most of the time the calls are closed without problem; however, sometimes I get a "Safe handle has been closed exception" when trying to make a new call. I handle the exception so that the program does not crash, but sometime the program hangs because a call is still "On hook" on the modem.

Here's the method I'm using to hang up the calls

        // hang up and close the
         public void hangUp()
        {
            try
            {
                // get the call and hangup and dispose of them ...
                if (phoneCall != null)
                {
                    phoneCall.BeginDrop(
                        delegate(IAsyncResult ar)
                        {
                            phoneCall.EndDrop(ar);
                        }, null);
                    modemLog.writeToLog("Hanging up ... ");
                }
            }
            catch (Exception ex)
            {
                modemLog.writeToLog("There was a problem hangin up ... ");
                modemLog.writeToLog(ex.ToString() + " - " + ex.Message);
            }
            finally
            {
                this.dispatchCallEndedEvent();
            }
        }

Here's the function I'm using to make calls

        // call a person
        public void call(Person p)
        {
            try
            {
                // if no calls, or the last call is disconnected or the call is idle
                if (phoneCall == null || phoneCall.CallState == CallState.Disconnected || phoneCall.CallState == CallState.Idle) // if the phone is not busy
                {
                    modemLog.writeToLog("Calling " + p.firstName + " " + p.lastName + " (" + p.phoneNumber + ") ...");
                    phoneCall = selectedLine.Addresses[0].MakeCall(p.phoneNumber);
                    //phoneCall.Line.CallStateChanged += this.onCallStateChanged;
                    phoneCall.BeginMonitoringDigits(DigitModes.Dtmf, new EventHandler<DigitDetectedEventArgs>(this.onDigitPressed));
                    timeAllowedToConnect.Start();
                }
                else // otherwise, just wait for the call to hang up
                {
                    p.numberOfTimesCalled = p.numberOfTimesCalled - 1; // subtract one from the number of times called or this person will be skipped
                    if (phoneCall != null)
                    {
                        modemLog.writeToLog("The current call is something funky. " + phoneCall.CallState.ToString());
                    }
                    else
                    {
                        modemLog.writeToLog("The current call is something funky. ");
                    }
                }
            }
            catch (Exception ex)
            {
                modemLog.writeToLog(ex.Message);
                throw ex;
            }
        }

Any help would be great ...

Thanks, Tony