解决方案 »
- C# 获取鼠标指向控件句柄
- C#中SerialPort接了10个测温传感器,其接收事件只有一个,如果一个传感器出错,怎样不影响别的传感器正常接收
- MessageBox问题!大家帮帮忙!
- C# 任务栏活动WinForm窗口颜色变换,提示?
- 程序调试错误
- 如何在RICHTEXTBOX中显示DOC文件,PDF文件?
- DataWindow.NET2.0!!!!!!!!!!!!!!!
- *WEB FORM中关于DATAGRID的问题*
- 如何使用C#读取文本文件
- I want to save winword.document.content into access database?who could give me code?
- 如何通过chrome的窗口句柄获取html的内容
- 请教一个奇葩问题:MDI窗口中MdiClient背后的窗体背景色问题
代码写的真乱.
你这个东西在哪里执行的,也没放到线程里啊.
不要死循环sleep
private void displayStatus()
{
for (; ; System.Threading.Thread.Sleep(1000))
{
label7.Text = status;
}
}上面这段代码放到线程里了么,如果在线程里,那么label7就得用invoke了;如果没有在线程里,那么界面假死就很正常了
"线程间操作无效: 从不是创建控件“label7”的线程访问它。
还是不行
http://blog.csdn.net/qq_16488883/article/details/35263571
我把代码弄上来这里了,谢谢啦
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的内容是变化的
我用的是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里面的事件
不对啊,连上服务器就会假死,不把SSH的部分注释掉,就会假死
不对啊,连上服务器就会假死,不把SSH的部分注释掉,就会假死说明是sshClient方法引起的阻塞,那你把sshClient的方法也放进线程里
不对啊,连上服务器就会假死,不把SSH的部分注释掉,就会假死说明是sshClient方法引起的阻塞,那你把sshClient的方法也放进线程里怎么放?真心不懂,头都大了,我很新手的
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();
}));
通信是很慢的(比起CPU来说),你让主线程建立通信,一定会阻塞UI