.NET 飞机模拟器
用例名称:开始飞机模拟
范围:飞机飞行模拟器
级别:用户目标
主要参与者:用户
- 用户启动飞机模拟器
- 询问用户最大高度(天花板)
- 询问用户最小高度(地板)
- 飞机模拟器从空中开始位置,没有起飞或着陆
- 飞机上升到最大高度
- 飞机下降到最小高度
- 重复步骤 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
- User starts Airplane simulator
- Ask the user for a maximum height(ceiling)
- Ask the user for a minimum height(floor)
- Airplane simulator begins from an airborne position, no takeoff or landing
- Airplane ascends to maximum height
- Airplane descends to minimun height
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 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.
由于这是一个控制台应用程序,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.