C# combox选定索引上的10秒计时器

C# combox选定索引上的10秒计时器,c#,events,timer,C#,Events,Timer,在很久没有编写脚本之后,我决定学习一种编程语言,我选择了C。我相处得很好,但现在我似乎第一次遇到了一个我无法用谷歌解决的问题 我正在制作一个模拟飞机系统作为学习练习,当从下拉组合框中选择一个选项时,我想调用一个循环 我有一个组合框/列表,其中有三个选项模拟启动机开关,数值为(0)关闭,(1)打开,(2)仅点火。在真正的飞机上,当选择“开”时,开关锁定到位10秒,然后释放。因此,我努力实现的目标是: private void comboBox2_SelectedIndexChanged(objec

在很久没有编写脚本之后,我决定学习一种编程语言,我选择了C。我相处得很好,但现在我似乎第一次遇到了一个我无法用谷歌解决的问题

我正在制作一个模拟飞机系统作为学习练习,当从下拉组合框中选择一个选项时,我想调用一个循环

我有一个组合框/列表,其中有三个选项模拟启动机开关,数值为(0)关闭,(1)打开,(2)仅点火。在真正的飞机上,当选择“开”时,开关锁定到位10秒,然后释放。因此,我努力实现的目标是:

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
   if (starterRight.SelectedIndex == 0)
   {
      //Starter is off
      eng2Start.Value = 0;
   }

   if (starterRight.SelectedIndex == 1)
   {
      //starter is on 
      //Start Timer
      eng2Start.Value = 1;

      if (eng2Tourqe >= 6000)
      {
         //open fuel valve
         // set Hot Start counter to 0
      }
      else 
      {
         //ensure fuel valve stays closed
         // set Hot Start counter to x+1
      }

      // End of Timer

      // set selected index back to 0
      (starterRight.SelectedIndex == 0)

   }
}
我一直在谷歌上搜索,我读得越多,我就越是迷失在这个世界里。我已经找到了包含大量代码的答案,而我目前还不能完全破译这些代码

有可能做我想做的事吗


提前感谢您抽出时间

您可以通过将其设置为true(或selected,或任何您想要的)睡眠10秒来实现这一点,如下所示:

Thread.Sleep(10000) ;
然后将其设置回false(或unselect,或任何您想要的)


另一种方法是启动一个将休眠10秒的后台线程,然后调用一个将“取消设置”按钮的方法,这样就不会阻塞GUI

或者,根据您使用的是什么,我可能会想出其他选择,但我认为您正在尝试学习基本的atm…:)

看到这个了吗


你可以使用3d.Sleep(10000)

您可以将
计时器
添加到
表单
,并将
间隔
属性设置为
10000
(10秒)

从代码:

if (starterRight.SelectedIndex == 1)
{
 //starter is on 
 //Start Timer
 timer1.Enabled=true;    
 }

 //in timer tick Event write the following:
 private void timer1_Tick(object sender, EventArgs e)
 {
   timer1.Enabled=false;
   //Statements to start aircraft 
 }

我认为这应该行得通。我没有编译它,但是用这个你锁定开关,做你的事情检查一个计时器,当到达10秒时,重新启用你的开关

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
   if (starterRight.SelectedIndex == 0)
   {
      //Starter is off
      eng2Start.Value = 0;
   }

   if (starterRight.SelectedIndex == 1)
   {
      //starter is on 

      starterRight.enable = false;

      StopWatch sw = new StopWatch();      

      sw.Start();
      eng2Start.Value = 1;


      if (eng2Tourqe >= 6000)
      {
         //open fuel valve
         // set Hot Start counter to 0
      }
      else 
      {
         //ensure fuel valve stays closed
         // set Hot Start counter to x+1
      }

      if (sw.ElapsedMilliseconds <= 10000)
      {
         do
         {
             //Dummy Loop
         }
         while (sw.ElapsedMilliseconds > 10000)
         sw.Stop(); 
      }
      else
      {
          // set selected index back to 0
          sw.Stop();
          starterRight.Enabled = true;
          (starterRight.SelectedIndex == 0)
      }

   }
}
private void组合框2\u SelectedIndexChanged(对象发送方,事件参数e)
{
如果(starterRight.SelectedIndex==0)
{
//起动机熄火了
eng2Start.Value=0;
}
如果(starterRight.SelectedIndex==1)
{
//起动机开着
starterRight.enable=false;
秒表sw=新秒表();
sw.Start();
eng2Start.Value=1;
如果(英语教程>=6000)
{
//打开燃油阀
//将热启动计数器设置为0
}
其他的
{
//确保燃油阀保持关闭状态
//将热启动计数器设置为x+1
}
如果(sw.ElapsedMill秒10000)
sw.Stop();
}
其他的
{
//将所选索引设置回0
sw.Stop();
starterRight.Enabled=true;
(starterRight.SelectedIndex==0)
}
}
}
您可以使用和事件。禁用开关,当计时器滴答作响时,将其启用

Timer timerSwitchOn;

public SomeConstructor()
{
    timerSwitchOn = new Timer(){Interval = 10*1000}; // 10 seconds
    timerSwitchOn.Tick += new EventHandler(timerSwitchOn_Tick);
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{    
   if (starterRight.SelectedIndex == 1)
   {
      //starter is on 
      starterRight.Enabled = false;
      timerSwitchOn.Start();
   }
}

void timerSwitchOn_Tick(object sender, EventArgs e)
{
    timerSwitchOn.Stop();
    starterRight.Enabled = true;

    // set selected index back to 0
    starterRight.SelectedIndex = 0;
}

在我最初的问题中,我说我想调用一个循环,我的意思是一个计时器,但似乎你们都明白了


谢谢你的快速回答,我将在这个周末陷入其中,看看我是否能解决我的问题

当从组合框中选择On时,您想等待10秒吗?这将阻止操作10秒,不确定OP是否需要,否则需要线程侦听器True,但他说它会锁定10秒…:)它不会锁定开关,它会阻止整个UI。很确定这不是OP的意图。