110,535
社区成员
发帖
与我相关
我的任务
分享
private string status = "Waiting for command";
private void send( string data1, stringdata2)
{
try
{
status = "Attempting to server....";
TAT = true;
timeRemaining = int.Parse(time);
System.Threading.Thread dThread = new System.Threading.Thread(new System.Threading.ThreadStart(countDown));
dThread.IsBackground = true;
dThread.Start();
}
catch
{
TAT = false;
MessageBox.Show("Connection error");
}
}
private void displayStatus()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
label7.Text = status;
}
}
private void countDown()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
try
{
if (TAT)
{
timeRemaining--;
status = string.Format("Attack has {0} time remainging", timeRemaining);
if (timeRemaining <= 0)
{
TAT = false;
break;
}
}
}
catch
{
}
}
}
this.label7.Invoke(new Action(()=>label7.Text = status));
private void displayStatus()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
label7.Text = status;
}
}
上面这段代码放到线程里了么,如果在线程里,那么label7就得用invoke了;如果没有在线程里,那么界面假死就很正常了 this.label7.Invoke(new Action(()=> label7.Text = status));
这句有问题,如果你只是想更新UI控件状态,应该用BeginInvoke而不是Invoke ,Invoke 会等待主线程界面控件状态更新后,再继续执行,调用者线程将被阻塞。
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();
}));
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里面的事件
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的内容是变化的