tags:

views:

74

answers:

2

Use Case Name: Start airplane simulation

Scope: Airplane Flight Simulator

Level: User goal

Primary Actor: User

  1. User starts Airplane simulator
  2. Ask the user for a maximum height(ceiling)
  3. Ask the user for a minimum height(floor)
  4. Airplane simulator begins from an airborne position, no takeoff or landing
  5. Airplane ascends to maximum height
  6. Airplane descends to minimun height
  7. Repeate steps 5 and 6, until user ends simulation

Here is my question. In .NET, which Timer best fits the Airplane class, should it be a Windows Forms timer, A server-based timer or a Threading Timer? I am trying to get the airplane to ascend/descend at a rate determined by the interval of the timer. Hope that makes sense.

I need some clarification on this, please help! Here is my class

using System; using System.Timers;

namespace 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--;                
        }            
    }


}

}

A: 

You should use System.Timers.Timer, for the reasons described here:

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

Basically, you are at the mercy of the rest of your Winforms code with the a Winforms Timer control, and it won't give you necessarily equal intervals.

David M
I am not necessarily concerned about equal time intervals. My goal is to 'simulate' a 'rate of increase or decrease in altitude'. My goal is to achieve a delay in increase or decrease. See my follow up question for further clarification, http://stackoverflow.com/questions/1866026/model-view-seperation-airplane-simulator
Berlioz
A: 

Since this is a console application a Windows Forms timer won't really work.

I would go with Threading timer.

Take note that multithreaded timing in a console application can be a little bit... silly. Your main thread will just sit in an infinite loop doing pretty much nothing until the other thread finishes.

rein
I intend on making this application either WPF or Silverlight. I am trying to achieve good Model-View separation. See my follow up question for further clarification, http://stackoverflow.com/questions/1866026/model-view-seperation-airplane-simulator
Berlioz