我写了个程序就是时间控制的,我想在3个时间段执行不同的程序,一个按钮点击后就让程序自动执行然后跳过时间以后程序自动关闭.部分代码如下
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //提交输入的时间与当前时间比再执行的程序
private void btnStart_Click(object sender, EventArgs e)
{
TimerExample te = new TimerExample();
te.Main();
} //这是个Timer 控件
private void btnTimer_Click(object sender, EventArgs e)
{
//while (true)
//{
TimerExample te = new TimerExample();
te.Main();
//}
} }
//执行自动时间控制类
public class TimerExample
{
public void Main()
{
System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer.
System.Threading.TimerCallback timerDelegate =
new System.Threading.TimerCallback(statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter. //Console.WriteLine("{0} Creating timer.\n",
// DateTime.Now.ToString("h:mm:ss.fff")); //获取当前小时
string hh = DateTime.Now.Hour.ToString();
//获取当前分
string mm = DateTime.Now.Minute.ToString();
//获取当前选项
// string choice = comboTime.Text;
//判断是否相等
if (hh + ":" + mm == "9:41")
{
//这里执行程序
// MessageBox.Show(hh + ":" + mm);
//这里是开启一个新的进程
Process.Start("http://www.yahoo.com.cn");
//continue;
}
else
{
//break;
}
System.Threading.Timer stateTimer =
new System.Threading.Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500); //Console.WriteLine("\nChanging period.\n");
if (hh + ":" + mm == "9:43")
{
//这里执行程序
//MessageBox.Show(hh + ":" + mm);
//这里是开启一个新的进程
Process.Start("http://www.you2home.com");
//continue;
}
else
{
//break;
}
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
//Console.WriteLine("\nDestroying timer."); if (hh + ":" + mm == "10:01")
{
//这里执行程序
//MessageBox.Show(hh + ":" + mm);
//这里是开启一个新的进程
ProcessStartInfo psi = new ProcessStartInfo("http://www.baidu.com");
Process.Start(psi); Process p = new Process(); psi=p.StartInfo;
}
else
{
//break;
}
} } public class StatusChecker
{
int invokeCount, maxCount; public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
} // This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
System.Threading.AutoResetEvent autoEvent = (System.Threading.AutoResetEvent)stateInfo;
//要执行的程序 //Console.WriteLine("{0} Checking status {1,2}.",
// DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString();//); if (invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //提交输入的时间与当前时间比再执行的程序
private void btnStart_Click(object sender, EventArgs e)
{
TimerExample te = new TimerExample();
te.Main();
} //这是个Timer 控件
private void btnTimer_Click(object sender, EventArgs e)
{
//while (true)
//{
TimerExample te = new TimerExample();
te.Main();
//}
} }
//执行自动时间控制类
public class TimerExample
{
public void Main()
{
System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10); // Create the delegate that invokes methods for the timer.
System.Threading.TimerCallback timerDelegate =
new System.Threading.TimerCallback(statusChecker.CheckStatus); // Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter. //Console.WriteLine("{0} Creating timer.\n",
// DateTime.Now.ToString("h:mm:ss.fff")); //获取当前小时
string hh = DateTime.Now.Hour.ToString();
//获取当前分
string mm = DateTime.Now.Minute.ToString();
//获取当前选项
// string choice = comboTime.Text;
//判断是否相等
if (hh + ":" + mm == "9:41")
{
//这里执行程序
// MessageBox.Show(hh + ":" + mm);
//这里是开启一个新的进程
Process.Start("http://www.yahoo.com.cn");
//continue;
}
else
{
//break;
}
System.Threading.Timer stateTimer =
new System.Threading.Timer(timerDelegate, autoEvent, 1000, 250); // When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500); //Console.WriteLine("\nChanging period.\n");
if (hh + ":" + mm == "9:43")
{
//这里执行程序
//MessageBox.Show(hh + ":" + mm);
//这里是开启一个新的进程
Process.Start("http://www.you2home.com");
//continue;
}
else
{
//break;
}
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
//Console.WriteLine("\nDestroying timer."); if (hh + ":" + mm == "10:01")
{
//这里执行程序
//MessageBox.Show(hh + ":" + mm);
//这里是开启一个新的进程
ProcessStartInfo psi = new ProcessStartInfo("http://www.baidu.com");
Process.Start(psi); Process p = new Process(); psi=p.StartInfo;
}
else
{
//break;
}
} } public class StatusChecker
{
int invokeCount, maxCount; public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
} // This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
System.Threading.AutoResetEvent autoEvent = (System.Threading.AutoResetEvent)stateInfo;
//要执行的程序 //Console.WriteLine("{0} Checking status {1,2}.",
// DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString();//); if (invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
程序路径 指定对应的.exe文件这个应该可以启动程序 只要你的时间控制好 这样就可以了我是小菜鸟! 说的不好清见谅, 希望能够帮到你!