0

Greeting,

  • OS: Windows 7 /64bit
  • Application: Visual Studio 2012 / C# and DotRas 1.3 Library

I am very new to c# or VS thing so please bear with me. After doing many hours R&D, I have finaly made a pppoe dialer program in C# / Dotras. This program have 3 main buttons

  1. Create /Add PPPoE Internet Dialer Connection in network connections , working fine
  2. Dial button, which connects the newly created dialer , working Fine
  3. Disconnect working fine

I have added StatuBox where dialup events should appear as showed on dotras youtube video tutorial (which was for vpn, but my project is for pppoe dialer)

StatusBox Not updating the dialup events like connecting/password error/connected etc. This is the part where I am finally confused.

Following is my Code.

// Dial Button Action
private void button2_Click_1(object sender, EventArgs e)
{
    using (RasDialer dialer = new RasDialer())
    {
        // I had to add below line to update statusTextBox Manualy , want to get rid of it by adding auto status update
        this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connection in progress ...", "{0}\r\n\r\n"));
        dialer.EntryName = ("pppoe2");
        string username = textBox1.Text;
        string passwd = textBox2.Text;
        // If username is empty dont connect 
        if (string.IsNullOrWhiteSpace(textBox1.Text))
        {
            this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Cancelled. Cannot continue with username/password.", "{0}\r\n"));
            MessageBox.Show("Enter username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }

        dialer.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
        dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
        dialer.Timeout = 1000;
        dialer.AllowUseStoredCredentials = true;
        // start dialing, 
        dialer.Dial();
        // If dialer connects successfully update StatuTextbox
        this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connected."));
    }
}

private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
{
    this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Status Changed"));
}

private void rasDialer1_Error(object sender, System.IO.ErrorEventArgs e)
{
    this.StatusTextBox.AppendText(string.Format("{0}\r\n", "STATUS UPDATE TEXT XYZ"));
}

private void rasDialer1_DialCompleted(object sender, DialCompletedEventArgs e)
{
    this.StatusTextBox.AppendText(string.Format("{0}\r\n", "STATUS UPDATE TEXT XYZ"));
}

Any help would be highly appreciable.

Nino
  • 6,931
  • 2
  • 27
  • 42
Syed Jahanzaib
  • 333
  • 6
  • 18
  • I don't see any code that attaches the `rasDialer1_StateChanged` event handler to the `StateChanged` event of your dialer. – stuartd Mar 17 '17 at 14:37
  • if you dont mind, Can you please provide an example please? – Syed Jahanzaib Mar 17 '17 at 14:57
  • 1
    No problem - `dialer.StateChanged += rasDialer1_StateChanged` after you create the dialer. Do the same for `Error` and `DialCompleted`. – stuartd Mar 17 '17 at 15:07
  • ok i modified the code as `dialer.Dial();` `dialer.StateChanged += rasDialer1_StateChanged;` and also added this in `Error` and `DialCompeted` but still dial events not updating in statustextbox. – Syed Jahanzaib Mar 17 '17 at 15:13
  • I think you should set the event handler before dialling – stuartd Mar 17 '17 at 16:00
  • A little progress have been made. Made some modifications. I use `rasDialer.DialAsync()` , and in ` private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)` section i used `MessageBox.Show(e.State.ToString(), "xxxxxxxxxxxx", MessageBoxButtons.OK);` . This works fine but it shows every single line messages in each MESSAGE BOX and i ahve to click on OK on every box. and if try to update statusText box using below line. i get error `this.StatusTextBox.AppendText(string.Format(e.State.ToString()));` – Syed Jahanzaib Mar 18 '17 at 10:06
  • If i use `this.StatusTextBox.AppendText(string.Format(e.State.ToString()));` then I am getting error `Cross-thread operation not valid: Control 'StatusTextBox' accessed from a thread other than the thread it was created on.` – Syed Jahanzaib Mar 18 '17 at 10:55
  • See [this question](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – stuartd Mar 18 '17 at 15:42
  • ok , In my C# WPF, i have dial button which dials specific pppoe dialer, How can i add check if this dialer is already connected , if yes then show message already connected, otherwise proceed connect it? – Syed Jahanzaib Mar 19 '17 at 10:00

2 Answers2

0

Ok I have managed to enable status box text append work fine.

this.Invoke((MethodInvoker)delegate
{
this.StatusTextBox.AppendText(string.Format(e.State.ToString() + "\r\n"));
});
}
Syed Jahanzaib
  • 333
  • 6
  • 18
0

Depending on how you were using it, if the background thread from the OS isn’t marshaled back to the UI thread it won’t update. Similarly to what you had done, the SynchronizingObject property on the RasDialer can be set to your form and the thread synchronization will occur automatically.

Jeff Winn
  • 804
  • 8
  • 14