c# - C# - 从另一个方法停止计时器
问题描述
我正在尝试用 C# 学习 OOP。目标框架:.net core 3.1。我有两节课。在第一个中,我初始化计时器:
public class Test
{
Timer timer = new Timer();
public void Begin()
{
timer.Elapsed += new ElapsedEventHandler(Action);
timer.Interval = 1000;
timer.Start();
}
public virtual void Action(Object source, ElapsedEventArgs e)
{
Console.WriteLine("Running");
}
public void Finish()
{
timer.Stop();
timer.Dispose();
}
}
在主类中,我有两种方法用于启动计时器和停止计时器
class Program
{
static void Main(string[] args)
{
Start();
ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
Console.WriteLine("Stopping");
Stop();
Console.ReadLine();
}
static void Start()
{
Test a = new Test();
a.Begin();
Console.WriteLine("Started");
}
static void Stop()
{
Test a = new Test();
a.Finish();
Console.WriteLine("Stopped");
}
}
为什么定时器在停止和处理后仍然运行?
解决方案
你很亲近!正如 Chamika 在评论中指出的那样,您正在与Test
班级的两个不同实例进行交互。new
关键字的意思是,new
创建一个实例Test
。
要更正此问题,您可以创建 aglobal variable
或将实例传递给您的Start()
andStop()
方法。
首先,我将展示global variable
解决方案。
没有编辑你的Test
课程,我修改了你的Program
课程。我创建了一个global variable
: myTimer
。myTimer
设置为Test
类的新实例。我还从您的Start()
andStop()
方法中删除了其他实例创建。因为他们在同一个班级,所以无论设置为还是Program
,他们都可以与您互动!global variable
public
private
class Program
{
// Global variable to use within the class.
// - This maintains the correct/same instance of the Test() class
// - to interact with, starting/stopping.
public static Test myTimer = new Test();
public static void Main(string[] args)
{
Start();
ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
Console.WriteLine("Stopping");
Stop();
Console.ReadLine();
}
static void Start()
{
// I removed the Test a = new Test() line as it creates
// - a 'new' instance of the Test() class
// Set the instance to interact with to the global variable: myTimer
myTimer.Begin();
Console.WriteLine("Started");
}
static void Stop()
{
// I removed the Test a = new Test() line as it creates
// - a 'new' instance of the Test() class
// Set the instance to interact with to the global variable: myTimer
myTimer.Finish();
Console.WriteLine("Stopped");
}
}
// Output:
// Started
// Running
// Running
// Running
// ...
// Stopping
// Stopped
这是passing variable
另一种思考方式的解决方案!
进行了大约相同数量的更改,只是以不同的方式进行。类Test
仍然没有被触及或改变。
而不是创建 a global variable
,将 the 添加arguments
到您的,以便他们期望传递methods
一个实例。Test
在您的方法中创建实例Main
,然后传递变量。
class Program
{
public static void Main(string[] args)
{
// Create the instance of the 'Test' class.
// Since this is created in the 'Main' scope, passing it to
// Start() and Stop() will use this instance.
Test myTimer = new Test();
// Pass 'myTimer' to Start() to start timer
Start(myTimer);
ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
Console.WriteLine("Stopping");
// Pass 'myTimer' to Stop() to stop timer
Stop(myTimer);
Console.ReadLine();
}
static void Start(Test myTimer)
{
myTimer.Begin();
Console.WriteLine("Started");
}
static void Stop(Test myTimer)
{
myTimer.Finish();
Console.WriteLine("Stopped");
}
}
// Output:
// Started
// Running
// Running
// Running
// ...
// Stopping
// Stopped
推荐阅读
- mongodb - User.findOne() 函数抛出“,”预期错误
- android - 导航组件 - 导航片段时如何加载大数据?
- quantum-computing - Open QASM vs Qiskit 中的通用门 U(theta, phi, lamda)
- sql-server - 从 Azure 连接到本地托管的 SQL Server
- django - 具有多字符串参数的 Django 路由
- image - 任务 ':image_cropper:parseDebugLibraryResources' 执行失败
- reactjs - 用户配置文件未显示在 github 搜索中
- r - 替换引用命名向量的整个字符串
- javascript - 在 Javascript 中将游戏数据压缩或转换为短字符串密码(然后再返回)
- r - 使用带有数字 x 轴的 ggplot 在 R 中的 y 轴上绘制类别变量的比例