.NET 飞机模拟器

发布于 2024-08-13 11:40:59 字数 2024 浏览 3 评论 0原文

用例名称:开始飞机模拟

范围:飞机飞行模拟器

级别:用户目标

主要参与者:用户

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

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

我需要对此进行一些澄清,请帮忙!这是我

使用 System 的课程; 使用系统定时器;

命名空间 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--;                
        }            
    }


}

}

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


}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抹茶夏天i‖ 2024-08-20 11:41:00

您应该使用 System.Timers.Timer,原因如下:

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

基本上,您将受到 Winforms 代码的其余部分与 Winforms Timer 控件的支配,它不会给您带来一定的平等间隔。

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.

旧城烟雨 2024-08-20 11:41:00

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

我会选择线程计时器。

请注意,控制台应用程序中的多线程计时可能有点……愚蠢。您的主线程将处于无限循环中,几乎不执行任何操作,直到另一个线程完成为止。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文