This project is read-only.

EventHandling and form freezing problem

Apr 5, 2009 at 9:48 AM
Edited Apr 5, 2009 at 9:53 AM
Hi jeff, thanks a lot for your great effort.
I am using your component to help me in my c# project.
And I have some questions about it.

Firstly, is there any way to prevent form not being froze during connection.
Second, although I have carefully read and did what you sad, I couldn't monitor the current dialing up connection.
What I did;

I put RasDialer,RasConnectionWatcher on to form.
I am trying to dial an existing phonebook entry so i have written;
using (RasPhoneBook phoneBook = new RasPhoneBook())
        {
            phoneBook.Open();
            foreach (RasEntry entry in phoneBook.Entries)
            {
                 rasDialer1.EntryName = entry.Name;
                 rasDialer1.PhoneBookPath = phoneBook.Path;
                 rasDialer1.Dial();
                 break;
             }
          }
I have set the rasdilaer1 event functions  from the form designer properties window.
Dial_Completed : dial_Completed
Statechanged : state_Changed
Here they are;
 public void dialer_StateChanged(object sender, StateChangedEventArgs e)
        {
            MessageBox.Show(e.State.ToString());
        }
        public void dial_Completed(object sender, DialCompletedEventArgs e)
        {
            if (e.Cancelled)
                MessageBox.Show("Dial Cancelled");
            else if (e.Error != null)
                MessageBox.Show(e.Error.Message);
            else if (e.TimedOut)
                MessageBox.Show(e.TimedOut.ToString());
        }
in fact, it is not important what i wrote into these two functions, because when i put a breakpoint into them, i realised they are not reachable.
Then I tried RasConnectionWatcher, 

            RasHandle handle = null;
            foreach (RasConnection connection in rasDialer1.GetActiveConnections())
            {
                if (connection.EntryName == "myConnection")
                {
                    handle = connection.Handle;
                    break;
                }
            }
            watcher.Disconnected += new EventHandler<RasConnectionEventArgs>(this. watcher_Disconnected);
            watcher.Connected+= new EventHandler<RasConnectionEventArgs>(this. watcher_Connected);
            watcher.Handle = handle;
            watcher.EnableRaisingEvents = true;

and again same result, what is wrong with that?
Can you simply put an example about how to use rasdialer and rasconnectionwatcher on a form?
By the way, thanks a lot.

Apr 5, 2009 at 3:16 PM
Edited Apr 5, 2009 at 3:18 PM
"Firstly, is there any way to prevent form not being froze during connection."

You just need to set the SynchronizingObject property to the instance of the form you're using it on. Since it's a multi-threaded object the synchronizing object is used to marshal events raised properly so you don't have that issue. RasDialer and RasConnectionWatcher both have that property available.

rasDialer1.SynchronizingObject = this;

"Second, although I have carefully read and did what you sad, I couldn't monitor the current dialing up connection."

Those events you're using for the dialer are only used when you're asynchronously dialing a connection. If you'd like to receive event notifications and not block your application while the dialing is in progress use the DialAsync method on the dialer rather than Dial. Also, when using the RasDialer (as stated in the class help documentation) it is imperative that you properly dispose of that object. If your application crashes while you're dialing a connection asynchronously Windows will not be able to cancel the connection for you. Disposing of the object will ensure that if the object is dialing a connection when it's being disposed of, it will force the connection attempt to cancel.

rasDialer1.DialAsync();
Apr 6, 2009 at 5:27 AM
Edited Apr 6, 2009 at 5:29 AM
Just a couple hints about the dialer:

1) You do not need to open a phone book if you already know the name of the entry you wish to dial. 

rasDialer1.EntryName = "My Entry";
rasDialer1.DialAsync();

2) As long as the entry is in either the all user's or current user's profile you do not need to specify the path to the phone book. A null reference for that property indicates the default phone book should be used.

rasDialer1.PhoneBookPath = null;

3) The way you're using a phone book to get the connection name to dial could cause problems. If another connection has been created on the machine and is loaded into the collection before the one you actually want the dialer to connect to you could cause your application to connect to the wrong entry.

The RasConnectionWatcher is designed to provide event notification (without polling) when connection state changes. This state is not as in depth as the state provided by the dialer. It merely monitors for connection, and disconnection states. If you provide a handle you can also monitor bandwidth added/removed along with only receiving disconnection notices for that handle.
Apr 7, 2009 at 8:16 AM
Thanks for your detailed answer Jeff, and one more question; can i detect the connection status or simply how do i know did the connection succeed without using the RasConnectionWatcher?
as i said before dial_completed is not working in that case.
Apr 7, 2009 at 2:43 PM
You need to dial the connection asynchronously as I said earlier to use the events on the RasDialer component. You need to call the rasDialer1.DialAsync(); method and you shouldn't be using the RasConnectionWatcher component unless you want to know whether a connection was disconnected so you can have your application reconnect the entry.

You can use the StateChanged event and look for the Connected state in the event.

private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
{
    if (e.State == RasConnectionState.Connected)
    {
        // The connection has completed successfully.
    }
}

e.State == RasConnectionState.Connected is only allowed when the connection negotiation has completed successfully. The DialCompleted event simply lets the caller know when the call has been completed, it doesn't know about the connection state. Whenever the call completes, whether it's successful or unsuccessful it will be raised. I suppose I could add another property to the DialCompletedEventArgs class so the event does know whether the connection succeeded.
Apr 8, 2009 at 2:09 PM
Just thought you might like to know - changeset 30823 adds the connected property to the DialCompletedEventArgs class used by the DialCompleted event. If you want to know whether the connection attempt was successful you no longer have to monitor the state yourself, you can use the DialCompleted event and check that property.
Apr 8, 2009 at 4:47 PM
Thanks for your reply, I will check it out.
Jun 24, 2009 at 8:14 AM

Jeff, are you there?

I have returned to thank you,

Coordinator
Apr 7 at 4:43 PM<abbr />

<input id="ctl00_ctl00_MasterContent_Content_PostRepeater_ctl08_PostId" name="ctl00$ctl00$MasterContent$Content$PostRepeater$ctl08$PostId" type="hidden" value="176610" />

You need to dial the connection asynchronously as I said earlier to use the events on the RasDialer component. You need to call the rasDialer1.DialAsync(); method and you shouldn't be using the RasConnectionWatcher component unless you want to know whether a connection was disconnected so you can have your application reconnect the entry.

You can use the StateChanged event and look for the Connected state in the event.

private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
{
    if (e.State == RasConnectionState.Connected)
    {
        // The connection has completed successfully.
    }
}

e.State == RasConnectionState.Connected is only allowed when the connection negotiation has completed successfully. The DialCompleted event simply lets the caller know when the call has been completed, it doesn't know about the connection state. Whenever the call completes, whether it's successful or unsuccessful it will be raised. I suppose I could add another property to the DialCompletedEventArgs class so the event does know whether the connection succeeded.


worked for me..

Have a nice day..
Jun 24, 2009 at 6:27 PM

Glad to hear it. :)