Can someone give me a heads up... I'm trying to use the dotRAS .NET control, and this code to change the value of internetConnected
(boolean) using an event handler...
But it seems that the state RasConnectionState.Disconnected
is not triggered by dotRAS hangup()..
Any ideas? Am I doing it totally wrong... or have I managed to find a bug?
public class USBModem
{
// private vars
private RasDialer dialer = new RasDialer();
private bool internetConnected = false;
/// <summary>
/// Default constructor for USBModem
/// </summary>
public USBModem()
{
// Add Events for dialer
dialer.StateChanged += new EventHandler<StateChangedEventArgs>(dialer_StateChanged);
}
void dialer_StateChanged(object sender, StateChangedEventArgs e)
{
// Handle state changes here
switch (e.State)
{
case RasConnectionState.Connected:
internetConnected = true;
Console.WriteLine(e.State.ToString());
break;
case RasConnectionState.Disconnected:
internetConnected = false;
Console.WriteLine(e.State.ToString());
break;
default:
Console.WriteLine("INFO -> Unhandled state: " + e.State.ToString());
break;
}
}
public void ConnectInternet(string connectionName)
{
// Dial
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
dialer.EntryName = connectionName;
dialer.DialAsync();
}
public void DisconnectInternet()
{
foreach (RasConnection connection in dialer.GetActiveConnections())
{
connection.HangUp();
}
}
}
Apparently, a very simple (but widespread) mistake.
Basically the RasDialer
component only handles events while a dialing operation is in progress.
The disconnected event would be raised if perhaps the modem line became unplugged during the connection attempt.
If you want to monitor client connections on the machine for connection/disconnection or a couple other events, use a RasConnectionWatcher
. This will receive notifications from Windows when connection changes are made outside of a dialing operation.
Documentation on dotRAS is particularly sparse on Google... Head to http://dotras.codeplex.com for further information. The Help files included with the SDK are also very useful.