This project is read-only.

HangUp Block the application For several Hour

Sep 13, 2012 at 5:14 AM
Edited Sep 13, 2012 at 9:18 AM

I have written a program using DotRas to make a dial up connection. 

The program is scheduled using Task Scheduler in Windows which will be triggered for every hour.

In each schedule, the program will make a dial up connection and send some files through FTP. At the end, the program required to hang up the connection. The FTP file upload process will be finished within 15 mins.

The following is the hangup procedure that I have written.

var conn = RasConnection.GetActiveConnections().Where(con => con.EntryName == ConnectionName).FirstOrDefault();

conn.HangUp();

Note that there is only one dialup connection in the windows.

The program blocked at the conn.HangUp() method and never returned. When I check the connection adapter in Windows, the dialup connection is still connected.

Is there any way to solve the above problem?

Sep 15, 2012 at 9:09 PM

Unfortunately the issue is more than likely the underlying RasHangUp call was returning something other than success to the caller, which would cause an infinite loop. There is no logging in 1.2 that will expose the result of the call, so you'll need to grab the project source code, reference it, and then step through it to see what the problem is.

Wish I had a better answer, but until 1.3 gets released which introduces logging in there isn't much else I can advise you to do. If you do decide to try and figure out what the result is, let me know so I can decide whether to check for it when deciding to continue looping in the HangUp method when it checks the result. The method in question is the DotRas/Internal/RasHelper.cs IsConnectionActive method. That one determines whether or not to continue checking whether the connection is active.

Let me know what you find,

- Jeff

Feb 14, 2013 at 12:03 PM
Edited Feb 14, 2013 at 12:34 PM
Hello,

I found why this is happening.

In the rather helpful MSDN link here, it says:
RAS supports referenced connections. If the entry being dialed is already connected, RasDial returns SUCCESS and the connection is referenced. To disconnect the connection, each RasDial on the connection should be matched by a RasHangUp.
This can easily be tested with the CreateAndDialVpnEntry WinForms example. If the vpn connection is disconnected when we press "Dial", it connects and later, when we press "Disconnect" it really closes the connection. When we enable the "Disconnect" button before start of app and we have Connected connection, pressing "Disconnect" without pressing "Dial" before that also closes the connection. But when we have opened the connection and then try "Dial" then "Disconnect" the app hangs.

This means that if we call RasHangUp for our RasDial, the connection won't be disconnected until RasHangUp is called the same number of times RasDial is called with that entry.

I'm sure that Stanley's app calls RasDial AFTER the connection is already Connected with another (may be non-programmic) call to RasDial, and that's why the application loops waiting for the connection's handle to become INVALID (or more like the IsConnectionActive method to return false). But this won't happen unless we call RasHangUp the number of times RasDial is called.

The easy (and working) fix is that we add in the public void HangUp(RasHandle handle, int pollingInterval) method's while loop this line:
    SafeNativeMethods.Instance.HangUp(handle);
at the end. This way, we'll surely disconnect the connection, but if any other app needs it, it won't be available.
Feb 17, 2013 at 7:43 AM
At first I wasn't so sure about your proposed change, but after thinking about it a bit more I understand what you're saying. Going to open a new work item for the change so I can track its progress.

Thank you for the suggestion!
Jeff
Feb 17, 2013 at 7:50 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.