2009-12-03 132 views
0

使用案例名称:启动飞机模拟.NET飞机模拟器

范围:飞机飞行模拟器

级别:用户目标

主要演员:用户

  1. 用户启动飞机模拟器
  2. 询问用户最大高度(上限)
  3. 询问用户的最小高度(地板)
  4. 飞机模拟器从机载位置,没有起飞或着陆
  5. 飞机开始上升到最大高度
  6. 飞机下降到minimun高度
  7. Repeate步骤5和6 ,直到用户结束模拟

这是我的问题。在.NET中,哪个Timer最适合Airplane类,它应该是Windows Forms定时器,基于服务器的定时器还是线程定时器?我试图让飞机以定时器间隔确定的速率上升/下降。希望这是有道理的。

我需要一些解释,请帮助!这里是我的班级

使用系统;使用System.Timers的 ;

命名ConsoleApplication1

{

class Airplane 
{ 
    public Airplane() 
    { 
     _currentAltitude = 0; 
     Timer _timer = new Timer();    
     _timer.Start(); 
     Console.WriteLine("airplane started"); 
     Console.ReadKey(); 
    } 

    public const int MAXALLOWABLEHEIGHT = 30000; 
    public const int MINALLOWABLEHEIGHT = 15000; 

    private int _currentAltitude;   

    public int CurrentAltitude 
    { 
     get 
     { 
      return _currentAltitude; 
     } 
     set 
     { 
      _currentAltitude = value; 
     } 
    } 


    private bool airplaneIsDead = false; 

    // Define the delegate types 
    public delegate void GoneTooHigh(string msg); 
    public delegate void GoneTooLow(string msg); 

    // Define member variables of the above delegate types 
    private GoneTooHigh MaxHeightViolationList; 
    private GoneTooLow MinHeightVioloationList; 



    // Add members to the invocation lists using helper methods 
    public void OnGoneTooHigh(GoneTooHigh clientMethod) 
    { 
     MaxHeightViolationList = clientMethod;    
    } 

    public void OnGoneTooLow(GoneTooLow clientMethod) 
    { 
     MinHeightVioloationList = clientMethod; 
    } 


    void _timer_Elapsed(object sender, ElapsedEventArgs e) 
    {       
     if (_currentAltitude < MAXALLOWABLEHEIGHT) 
     {    
      _currentAltitude++;     
     } 
     else 
     { 
      _currentAltitude--;     
     }    
    } 


} 

}

回答

0

您应该使用System.Timers.Timer,此处描述的原因:基本上

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

,你受到其他人的摆布你的WinForms代码与Winforms Timer控件一起使用,它不会给你一定的时间间隔。

+0

我不一定关心等时间间隔。我的目标是'模拟''高度增加或减少的速度'。我的目标是延迟增加或减少。看到我的后续问题进一步澄清, http://stackoverflow.com/questions/1866026/model-view-seperation-airplane-simulator – dannyrosalex 2009-12-08 10:24:24

0

由于这是一个控制台应用程序,Windows窗体计时器不会真正起作用。

我会去与线程计时器。

请注意,控制台应用程序中的多线程时序可能有点......愚蠢。你的主线程只会坐在一个无限循环中,直到另一个线程完成。

+0

我打算使此应用程序WPF或Silverlight。我试图实现良好的模型 - 视图分离。看到我的后续问题进一步澄清, http://stackoverflow.com/questions/1866026/model-view-seperation-airplane-simulator – dannyrosalex 2009-12-08 10:22:22