急!关于线程的小问题

jinglej 2012-07-26 11:04:43
public void startThread()
{
Thread th1 = new Thread(new ThreadStart(fun));
th1.IsBackground = true;//设为后台线程
th1.Start();
}
void fun()
{
for (int i=0; i<5; i++)
{
label1.text += i;
}
}
运行结果:label1上显示:01234.
我的问题:如何让label1上一直不停的循环显示01234,也就是让线程一直执行fun函数。
希望详细解答,本人新手!急!
...全文
176 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinglej 2012-08-02
  • 打赏
  • 举报
回复
问题解决了,但是还是不太明白跳不出方法的原因。这改改那改改就好了。谢谢各位了!
失落的神庙 2012-07-31
  • 打赏
  • 举报
回复
还有线程循环的时候注意要 Thread.sleep(100);随便延迟吧。不要太小就行。如果解决不了可加扣抠:腰陆49陆久148腰
kkai189 2012-07-31
  • 打赏
  • 举报
回复
明显代码fun中只有一个循环,而且有结束条件,参考2楼吧,你需要个死循环!
失落的神庙 2012-07-31
  • 打赏
  • 举报
回复
executeMultiCmds先看停在哪里。
是不是算正常。
如果是循环。判断是不是可能存在死循环。
如果不是。查查为何不跳出方法。
失落的神庙 2012-07-30
  • 打赏
  • 举报
回复
打断点。怎么执行的
Anod 2012-07-30
  • 打赏
  • 举报
回复

foreach (string i in time1)
{
if (Convert.ToDateTime(i).ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
{
runRestart1();
}
}



runRestart1();
是干什么的?应该是第一个时间检测到后执行它之后一直没有退出吧? 你输入时间时不要让第一个输入框的时间等于系统时间,让第三个或者第四个等于系统时间看执行情况。
jinglej 2012-07-30
  • 打赏
  • 举报
回复
还是不行。。。谢谢!
jinglej 2012-07-30
  • 打赏
  • 举报
回复
非常感谢,我找到问题了。但是不知道该怎么改,还请大家帮忙。
问题是:比如设置两个时间:12:04,12:05,则在12:04时执行一次runRestart1(),它先killTomcat1(),再startTomcat1(),但是它执行完startTomcat1()之后就停止了,也就是执行完startTomcat1()中的executeMultiCmds(str1, str2, str3)就停止了,没有跳出startTomcat1(),更没有跳出runRestart1()。
然后当手动关闭Tomcat窗口后,在12:05时又启动了Tomcat。
应该怎么让程序启动Tomcat后退出runRestart1()呢?
jinglej 2012-07-30
  • 打赏
  • 举报
回复
/// <summary>
/// 重启Tomcat1
/// </summary>
public void runRestart1()
{
killTomcat1();
Thread.Sleep(2000);
startTomcat1();
}

/// <summary>
/// 启动Tomcat1
/// </summary>
public void startTomcat1()
{
string str1 = @"set JAVA_HOME=" + tb_JavaInstall1.Text.ToString();
string str2 = @"set CATALINA_HOME=" + tb_TomcatExtract1.Text.ToString();
string str3 = tb_TomcatExtract1.Text.ToString() + @"\bin\startup.bat";

executeMultiCmds(str1, str2, str3);
}

/// <summary>
/// 关闭Tomcat1
/// </summary>
public void killTomcat1()
{
string str1 = @"set JAVA_HOME=" + tb_JavaInstall1.Text.ToString();
string str2 = @"set CATALINA_HOME=" + tb_TomcatExtract1.Text.ToString();
string str3 = tb_TomcatExtract1.Text.ToString() + @"\bin\shutdown.bat";

executeMultiCmds(str1, str2, str3);
}
失落的神庙 2012-07-28
  • 打赏
  • 举报
回复
不行的话用这个试试
在mian()函数加这行代码
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
jinglej 2012-07-26
  • 打赏
  • 举报
回复
不行,还是只执行一次!
失落的神庙 2012-07-26
  • 打赏
  • 举报
回复
public void listenTime1()
{
while (true)
{
Thread.Sleep(100);
foreach (string i in time1)
{
if (Convert.ToDateTime(i).ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
{
runRestart1();
}
}
}
}
试试
jinglej 2012-07-26
  • 打赏
  • 举报
回复
楼上的正解,但是帮我看看这样为什么不行。
窗体上放了四个TextBox输入四个时间,通过Button提交修改后的时间,这四个时间存入time1数组,当系统时间与这四个时间相同是就执行runRestart1();
我的问题:只在第一个TextBox显示的时间时执行了runRestart1();其他三个时间都不执行。我想让他执行。
我调试看了一下修改的时间是成功的,time1存入了新修改的值。
private Thread th1;
private string[] time1 = new string[4];
public Form1()
{
InitializeComponent();
Initialize();
}

void Initialize()
{
startThread();
}

public void startThread()
{
th1 = new Thread(new ThreadStart(listenTime1));
th1.IsBackground = true;//设为后台线程
th1.Start();
}

public void listenTime1()
{
while (true)
{
foreach (string i in time1)
{
if (Convert.ToDateTime(i).ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
{
runRestart1();
}
}
}
}

private void btn_ConfirmTime1_Click(object sender, EventArgs e)
{
try
{
Properties.Settings.Default.time1 = tb_RestartTime1.Text.ToString();
Properties.Settings.Default.time2 = tb_RestartTime2.Text.ToString();
Properties.Settings.Default.time3 = tb_RestartTime3.Text.ToString();
Properties.Settings.Default.time4 = tb_RestartTime4.Text.ToString();
Properties.Settings.Default.Save();

time1[0] = tb_RestartTime1.Text.ToString();
time1[1] = tb_RestartTime2.Text.ToString();
time1[2] = tb_RestartTime3.Text.ToString();
time1[3] = tb_RestartTime4.Text.ToString();

MessageBox.Show("时间设置成功!");
}
catch (Exception ex)
{
ex.ToString();
}

}
bdmh 2012-07-26
  • 打赏
  • 举报
回复
void fun()
{
while(true)
{
for (int i=0; i<5; i++)
{
label1.text += i;
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧