【问题标题】:c# with dotras - Status Box not updating automatically after dial connect/disconnectc# with dotras - 拨号连接/断开连接后状态框不会自动更新
【发布时间】:2017-03-17 14:33:06
【问题描述】:

问候,

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

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

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

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

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

任何帮助都将不胜感激。

【问题讨论】:

  • 我没有看到任何将rasDialer1_StateChanged 事件处理程序附加到您的拨号器的StateChanged 事件的代码。
  • 如果你不介意,你能提供一个例子吗?
  • 没问题 - dialer.StateChanged += rasDialer1_StateChanged 创建拨号程序后。对 ErrorDialCompleted 执行相同操作。
  • 好的,我将代码修改为 dialer.Dial(); dialer.StateChanged += rasDialer1_StateChanged; 并在 ErrorDialCompeted 中添加了此代码,但仍然拨打状态文本框中未更新的事件。
  • 我认为你应该在拨号前设置事件处理程序

标签: c# visual-studio-2012 dotras


【解决方案1】:

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

this.Invoke((MethodInvoker)delegate
{
this.StatusTextBox.AppendText(string.Format(e.State.ToString() + "\r\n"));
});
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多