解决方案 »

  1.   

     private void displayStatus()         {             for (; ; System.Threading.Thread.Sleep(1000))             {                 label7.Text = status;             }         } 
    代码写的真乱.
    你这个东西在哪里执行的,也没放到线程里啊.
      

  2.   

    想显示,用timer
    不要死循环sleep
      

  3.   

    http://bbs.csdn.net/topics/390820673
      

  4.   

    界面假死一般是由于 UI线程执行耗时操作,或者跨线程访问UI控件导致
      

  5.   


            private void displayStatus()
            {
                for (; ; System.Threading.Thread.Sleep(1000))
                {
                    label7.Text = status;
                }
            }上面这段代码放到线程里了么,如果在线程里,那么label7就得用invoke了;如果没有在线程里,那么界面假死就很正常了
      

  6.   


    "线程间操作无效: 从不是创建控件“label7”的线程访问它。
      

  7.   

    用invokethis.label7.Invoke(new Action(()=>label7.Text = status));
      

  8.   

    把label7.Text = status;改为label7.Invoke(new Action(()=>label7.Text = status));?
    还是不行
      

  9.   

    label7.Invoke(new Action(()=>label7.Text = status));这条语句没问题啊,方便的话,你把代码贴出来或是上传到一个地方,我帮你看看
      

  10.   


    http://blog.csdn.net/qq_16488883/article/details/35263571
    我把代码弄上来这里了,谢谢啦
      

  11.   

    对,是的,然后lable7.txt是显示连接状态和时间的
      

  12.   


              int timeRemaining = 0;
            static List<string> leagueList;
            private bool bAttacking = false;
            private string status = "Waiting for command";
            private void sendDrop(string ip, string port, string pSize, string time)
            {
                try
                {
                    status = "Attempting to connect to server....";
                    //SshClient sshClient = new SshClient("1111", "222", "3333");
                    //sshClient.Connect();
                    bAttacking = true;
                    timeRemaining = int.Parse(time);
                    System.Threading.Thread dThread = new System.Threading.Thread(new System.Threading.ThreadStart(countDown));
                    dThread.IsBackground = true;
                    dThread.Start();
                    //sshClient.RunCommand(string.Format("ads.pl {0} {1} {2} {3}", ip, port, pSize, time));
                    //sshClient.Disconnect();
                }
                catch 
                {
                    bAttacking = false;
                    MessageBox.Show("Connection error");
                }
            }
            private void displayStatus()
            {
                for (; ; System.Threading.Thread.Sleep(1000))
                {
                   this.label7.Invoke(new Action(()=> label7.Text = status));
                }
            }
            private void countDown()
            {
                for (; ; System.Threading.Thread.Sleep(1000))
                {
                    try
                    {
                        if (bAttacking)
                        {
                            timeRemaining--;
                            status = string.Format("Attack has {0} time remainging", timeRemaining);
                            if (timeRemaining <= 0)
                            {
                                break;
                            }
                            //bAttacking = false;
                        }
                        //bAttacking = false;
                    }
                    catch
                    {
                    }
                }
            }
            private void Form3_Load(object sender, EventArgs e)
            {
                System.Threading.Thread dThread = new System.Threading.Thread(new System.Threading.ThreadStart(displayStatus));
                dThread.IsBackground = true;
                dThread.Start();
            }        private void button1_Click(object sender, EventArgs e)
            {
                sendDrop("", "", "", "1000");
            }
    帮你调了下,不太明白为什么要把countDown()方法里的bAttacking设置为false,所以我就把bAttacking = false;注释掉了,在vs2010下测试界面里label的内容是变化的
      

  13.   

    你设计的初衷应该是想通过bAttacking来控制status的变化,可是你把bAttacking设置为false之后就没有再改变bAttacking的状态,所以label的内容就不会发生改变
      

  14.   


    我用的是2013,还是不行,除非时间到,不然一直假死状态,不能动。
     private void button1_Click(object sender, EventArgs e)
            {
                if (checkLad())
                {
                    if (!bAttacking)
                    {
                        sendDrop(darta1,data2, "data3", data4);
                    }
                    else
                        MessageBox.Show("Please wait");
                }
                else MessageBox.Show("Please got");
            }
    这事botton里面的事件
      

  15.   


    不对啊,连上服务器就会假死,不把SSH的部分注释掉,就会假死
      

  16.   


    不对啊,连上服务器就会假死,不把SSH的部分注释掉,就会假死说明是sshClient方法引起的阻塞,那你把sshClient的方法也放进线程里
      

  17.   


    不对啊,连上服务器就会假死,不把SSH的部分注释掉,就会假死说明是sshClient方法引起的阻塞,那你把sshClient的方法也放进线程里怎么放?真心不懂,头都大了,我很新手的
      

  18.   


                    Thread thread = new Thread(new ThreadStart(() =>
                    {
                        SshClient sshClient = new SshClient("1111", "222", "3333");
                        sshClient.Connect();
                        sshClient.RunCommand(string.Format("ads.pl {0} {1} {2} {3}", ip, port, pSize, time));
                        sshClient.Disconnect();
                    }));
      

  19.   

    所有通信相关的代码都应该放到线程里.
    通信是很慢的(比起CPU来说),你让主线程建立通信,一定会阻塞UI
      

  20.   

    上面代码:  this.label7.Invoke(new Action(()=> label7.Text = status)); 这句有问题,如果你只是想更新UI控件状态,应该用BeginInvoke而不是Invoke  ,Invoke 会等待主线程界面控件状态更新后,再继续执行,调用者线程将被阻塞。