HangUp runs for >5 minutes

May 3, 2011 at 4:19 PM


I have an intermittent problem which seems to occur when the dial-up GPRS modem has a connection problem. Here's the sequence of events:

  1. the application has 3 successive failures trying to synchronise with a back end database over an active GPRS connection
  2. RasConnection.GetActiveConnections() confirms that the connection is active
  3. connection.HangUp() throws the exception "Thread was being aborted" after just over 5 minutes
  4. RasConnection.GetActiveConnections() confirms that the connection is still active

Can anyone suggest how to handle this and whether I am doing anything wrong?


May 4, 2011 at 2:55 AM

Well the HangUp method is a synchronous call, so why it'd be throwing a ThreadAbortedException is unknown to me.

I'd probably start by asking...

  1. What do you have calling HangUp?
  2. Is it a background thread that's calling it?

I have seen various problems when using GRPS modems and RAS in Windows, though I haven't seen any problems specific with how DotRas interacts with RAS. I'd probably check your code to make sure you don't have something out of order if your application is multi-threaded. As for why your application might be seeing the connection being active, it sounds like something could have corrupted the RAS state machine. Make sure you're not terminating your application during development, and disposing of the RasDialer component properly. See the RasDialer component help information in the SDK for more information.

May 4, 2011 at 9:19 AM

Thanks for the quick response. The code to manage the GPRS connection all runs in a background thread, which is kicked off if the existing connection drops or there have been multiple failures of the database synchronisation. I'm embarrassed to admit that I've now spotted the code which aborts that background thread once its been running for 5 minutes, which explains the ThreadAbortedException and can be easily fixed as long as the HangUp method will eventually time out and let the thread finish cleanly.

The code always calls the Dispose method of the RasDialer component immediately. Also it's not a development environment issue - it's only ever happened on live Windows Embedded devices running in the field.

As you suggest something may have corrupted the RAS state machine - will the HangUp method eventually time out if the thread is left running?

Is there any way of refreshing the RAS state machine if it becomes corrupt?


May 4, 2011 at 11:01 AM

I've just had a look in the source, and it looks like HangUp will keep running as long as RasGetConnectStatus returns ERROR_SUCCESS i.e. it'll never finish if Windows gets itself into a bad state

May 6, 2011 at 1:46 AM


Right now there is no timeout on HangUp, if HangUp is called and the driver doesn't properly handle the request it could continue indefinitely. With that said, even if I introduced a timeout mechanism to the HangUp method the RAS state machine would have become corrupted and require a machine reboot to fix it since there is no way to cancel a HangUp operation once it's underway. Also, once the state manager has became corrupted, the only way I've found to fix it is to reboot the machine. Though I think that depends on the operating system involved, Windows 7 seems to handle those sorts of problems better than Windows XP. To my knowledge, there is no way to "fix" the state machine after it's corrupted.

The Windows SDK says to check whether a connection is active is by calling RasGetConnectStatus (which I've done) but break out of checking for it if the result is ERROR_INVALID_HANDLE. I took that one step further and said if any result but SUCCESS comes back go ahead and break out. There was a request quite some time ago to make that change because of some drivers not reporting their status properly back to RAS. Originally I was following the Windows SDK guidelines, which that changed on 10/18/2009 (changeset 45194). If you're curious about the discussion why this change was made... you can read about it here http://dotras.codeplex.com/discussions/52583. The problem talked about there seems to be pretty similar to what you're seeing. Unfortunately he never responded as to what the actual problem was.

If the modem driver isn't properly handling the HangUp request and closing the connection, there isn't much that I can do about that. You might need to go around RAS and send the command directly to the modem (not supported in DotRas) to terminate the connection.


May 10, 2011 at 7:14 PM

Hi Jeff,

I tried sending commands directly to the modem through the COM port, but Windows still has the port open so I get an exception 'Access to the port 'COM2' is denied'. So I've tried the more aggressive approach of disabling and re-enabling the modem using some code from http://www.codeproject.com/KB/cs/HardwareHelper.aspx. After that DotRas can make a new connection fine!

Thanks again for your support,