多线程可视化c的问题#

问题描述:

我正在使用ssh登录到linux设备。 然后我想每5秒钟监视一次连接状态。 我试图用后台工作来完成它,但是我在跨线程方面遇到了很多问题。 所以我手动创建了新的线程并使用委托来处理跨线程。多线程可视化c的问题#

但我没有工作得很好。 例如在登录UI堆栈几秒钟之前,直到登录完成。

这是我使用的登录代码:

private void login_button_Click(object sender, EventArgs e) 
    { 
     toolStripProgressBar1.Visible = true; 
     status.Text = "Connecting..."; 
     Thread t = new Thread(check_del_login); 
     t.IsBackground = true; 
     t.Start(); 
    } 
    delegate void del(); 
    private void check_del_login() 
    { 
     if (this.InvokeRequired) 
     { 
      del d = new del(login); 
      Invoke(d); 
     } 
     else 
     { 
      login(); 
     } 
    } 
    private void login() 
    { 
     ssh = new ssh_login(ip_addr, ssh_username, ssh_password); 
     f = new Form3(); 
     f.sftp(ip_addr, ssh_username, ssh_password);// Send the information to the new form 
     try 
     { 
      ssh.login1();//Connect 
      ssh.login2();//Connect 
     } 
     catch (Exception ex) 
     { 
      log(ex); 
     } 
     if (!ssh.check_login1() || !ssh.check_login2()) 
     { 
      MessageBox.Show("Could not login by SSH to " + ip_addr); 
      return; 
     } 
     try 
     { 
      f.login();// Login with sftp 
     } 
     catch (Exception ex) 
     { 
      log(ex); 
     } 
     if (!f.check_connection()) 
     { 
      MessageBox.Show("Could not login by web to " + ip_addr); 
      return; 
     } 
     toolStripProgressBar1.Visible = false;// hide the progress bar when the process finishes 
     if (ssh.check_login1() && ssh.check_login2() && f.check_connection()) 
     { 
      status.Text = "Connected"; 
      connection_status_timer.Start(); 
      status.Text = "Updating interface list..."; 
      Thread t = new Thread(check_del_update_interface); 
      t.IsBackground = true; 
      t.Start(); 
      show_form(); 
      hide_login(); 
     } 
     else 
     { 
      status.Text = "Disconnected"; 
      hide_form(); 
      show_login(); 
      comboBox_interface.Items.Clear(); 
      comboBox_interface.Items.Insert(0, "any"); 
      connection_status_timer.Stop(); 
     } 
    } 

我去了所有的导游,我可以找到上线,但我就是不明白我在做什么错。

在此先感谢。

+1

你走错了路。将线程代码与ui代码分开。越多越好。 – 2013-04-20 11:53:56

你的直接问题是check_del_login()它总是会调用()主要的login()方法。这意味着您的实际代码将再次在主线程上运行。

private void check_del_login() // is run on a thread 
{ 
    if (this.InvokeRequired)  // always true 
    { 
     del d = new del(login); // so, run login() on Main thread again 
     Invoke(d); 
    } 
    else 
    { 
     login(); 
    } 
} 

您应该简单地删除thos方法并直接在线程上运行login()。但是,然后你碰到(只是第一个):

toolStripProgressBar1.Visible = false;// hide the progress bar when the process finishes 

这种方法触及GUI,所以它应该被调用。你有很多类似的代码。

最佳建议:返回Backgroundworker并修复您的UI问题。