Feb 24, 2010 at 1:59 PM
Edited Feb 24, 2010 at 2:07 PM
Well, the issue is definitely in your code.
In your constructor for the object you're creating the instance of the RasDialer and wiring up the events. If you look inside your connect method, you're creating another new instance of that object and not wiring the events.
There's also a couple other things I've noticed:
1) When using events with .NET 2.0 or later you should be using the generic EventHandler(Of T) delegate to define the event.
public event EventHandler<GPRSArgs> EventStateChanged;
public event EventHandler<GPRSArgs> EventDialCompleted;
This is common practice and actually encouraged by the C# compiler since not using generics will cause a warning. In the end they work the same way as yours, you just don't need to define a ton of delegates all over the place to handle your events. When
working with events you should also check to make sure the event isn't null before calling it as shown in my example. When you were calling the event, you also passed a new object into the event when raising it. The sender object is the object which caused
the event, which is almost always the current object.
2) There are some issues when you're creating the entry.
When you're calling RasEntry.CreateDialUpEntry you're doing a couple things I discourage with that particular method. The "modem" string you have hard-coded there should be replaced with RasDeviceType.Modem which I have pulled from the Windows
SDK. If the value ever changes your application will be responsible of knowing the new value for each platform your application supports. Also, after the call you're overwriting all of the default settings that have been set within the method. This is why
I have the work item to change those to booleans, no one knows how to use flags in C#. The call needs to be |= unless you intend on overwriting all the default settings. Again, that method supports settings on every supported platform not just the one you're
3) You're not disposing of the RasDialer as instructed in the SDK.
This is a big one. After doing a lot of work with the Windows API and finding the nuances of working with RAS, if the RasDialer isn't disposed of properly once it finishes dialing or if your application crashes your application will REQUIRE a reboot
on the machine to release the connection before it can be used again. This includes if you call it from Windows or from your application. Since you're doing asynchronous dialing, you should definitely do something about this. Since you've got it defined at
class level, your best bet would be to implement IDisposable on the class and make sure you call it from your application before the Form or whatever is using it terminates.