0

问候,

  • 操作系统:Windows 7 /64位
  • 应用程序:Visual Studio 2012 / C# 和 DotRas 1.3 库

我对 c# 或 VS 很陌生,所以请多多包涵。经过数小时的研发,我终于用 C# / Dotras 制作了一个 pppoe 拨号程序。该程序有 3 个主要按钮

  1. 在网络连接中创建 /Add PPPoE Internet Dialer Connection ,工作正常
  2. 拨号按钮,连接新创建的拨号器,工作正常
  3. 断开连接工作正常

我添加了StatuBox,拨号事件应该出现在dotras youtube视频教程中(这是用于vpn,但我的项目是用于pppoe拨号器)

StatusBox 不更新拨号事件,如连接/密码错误/已连接等。这是我最终感到困惑的部分。

以下是我的代码

// 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"));
}

任何帮助都将是非常可观的。

4

2 回答 2

0

好的,我已经设法启用状态框文本附加工作正常。

this.Invoke((MethodInvoker)delegate
{
this.StatusTextBox.AppendText(string.Format(e.State.ToString() + "\r\n"));
});
}
于 2017-03-20T03:17:23.280 回答
0

根据您使用它的方式,如果操作系统的后台线程未编组回 UI 线程,则它不会更新。与您所做的类似,RasDialer 上的 SynchronizingObject 属性可以设置为您的表单,线程同步将自动发生。

于 2019-03-14T11:09:41.557 回答