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();
}
}
}
}
试试
楼上的正解,但是帮我看看这样为什么不行。
窗体上放了四个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();
}
}
}
}