我有一个程序可以禁用锁屏并在Windows中停止服务。我有两个按钮启用,禁用和一个具有预设时间的组合框。当我的程序运行并且用户单击启用时,程序应禁用锁定屏幕直到用户手动 点击禁用。我想要完成的是如果用户永远不会禁用,则让程序不能整夜运行。因此,通过从组合框中选择预设时间,程序将自动禁用它。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DateTime time = DateTime.Today;
for (DateTime _time = time.AddHours(16); _time < time.AddHours(18); _time = _time.AddMinutes(30))
{
comboBox1.Items.Add(_time.ToShortTimeString());
}
}
private static System.Timers.Timer _Timer;
private DateTime _lastRun = DateTime.Now;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string strTime_Start = DateTime.Today.ToString();
string strTime_End = comboBox1.SelectedItem.ToString();
}
public void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
_Timer = new System.Timers.Timer(10 * 60 * 1000);
_Timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
DisableLock();
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (strTime_End < DateTime.Now.Date) //I think this would be where I need to have strTime_End?
{
_Timer.Stop();
_lastRun = DateTime.Now;
}
}
}
答案 0 :(得分:1)
我认为最简单的解决方案是为您的停止时间保留一个实例变量,并且您设置的每个组合框项都设置了这个停止时间,timer_tick事件只会检查它是否已经过了那个时间。组合框中的空白项可以清除变量。
private DateTime timeToStop;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
timeToStop = DateTime.Now.Add(DateTime.Parse(comboBox1.Text));
}
catch(Exception)
{
timeToStop = new DateTime(3000, 01, 01, 00, 00, 00);
}
}
public void disableButton_Click(object sender, EventArgs e)
{
_Timer.Stop();
_lastRun = DateTime.Now;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now >= timeToStop)
{
_Timer.Stop();
_lastRun = DateTime.Now;
// Disable regkey
}
}
答案 1 :(得分:0)
从我到目前为止的理解,您可以添加:
comboBox1.Enabled = false;
当时间过去时,即在事件中。