在类文件中使用计时器控件 [英] Use timer control in class file

查看:70
本文介绍了在类文件中使用计时器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发窗口应用程序项目,并在我的项目中使用一个类文件.在该类文件中,我编写了一些方法和属性,并以其他形式使用了这些东西.现在,我想在类中使用Timer控件文件,因此我无法从工具箱中控制计时器,因此我如何初始化计时器及其类文件中的滴答声事件.
我只有.cs文件,没有形式的cs文件.我初始化并生成滴答事件,但没有通过计时器调用滴答事件,这是我的代码:
我在串行端口数据接收事件中启动计时器,但是当我以其他方法启动计时器时,它将正常工作.
void comPort_DataReceived(对象发送者,SerialDataReceivedEventArgs e)
{
tmr_rec = new Timer();
tmr_rec.Start();
tmr_rec.Interval = 1;
tmr_rec.Tick + =新的EventHandler(tmr_rec_Tick);
}
void tmr_rec_Tick(对象发送者,EventArgs e)
{
tmr_rec.Stop();
}

I am develop window application project and i and use one class file in my project.In that class file i am write some method and property and i use that things in other form.Now i want to use Timer control in my class file so i am not take Timer control from Tool Box so how i initialize timer and it''s tick event in my class file.
I have only .cs file not form''s cs file.I initialize and generate tick event but not call tick event by timer that''s problem.Here is my code:
I am start timer in serial port data receive event but when i start timer in other method then it will work proper.
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
tmr_rec = new Timer();
tmr_rec.Start();
tmr_rec.Interval = 1;
tmr_rec.Tick += new EventHandler(tmr_rec_Tick);
}
void tmr_rec_Tick(object sender, EventArgs e)
{
tmr_rec.Stop();
}

推荐答案

尝试将其定义为单独的.cs文件:请注意,我们正在使用单独的NameSpace:
Try this defined as a separate .cs file: Note we are using a separate NameSpace:
namespace ClassWithTimer
{
    using System;
    using System.Timers;

    public class TimerInClass
    {
        public Timer TheTimer = new Timer();

        private int timerCount;

        // constructor
        public TimerInClass(double interval, bool startNow)
        {
            timerCount = 0;

            // this is not really necessary, since the default value
            // of a new System.Timer's 'AutoReset Property is 'true: 
            // shown here only for educational value
            //
            // if AutoReset is set to 'false: the Elapsed EventHandler 
            // is called once: the System.Timer must be re-started to use again
            TheTimer.AutoReset = true;

            TheTimer.Interval = interval;

            TheTimer.Elapsed += new ElapsedEventHandler(TheTimer_Elapsed);

            TheTimer.Enabled = startNow;
        }

        public void TheTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            // test case
            Console.WriteLine(timerCount);
            timerCount++;
        }

        public void StartTimer()
        {
            TheTimer.Enabled = true;
        }

        public void StopTimer()
        {
            TheTimer.Enabled = false;
        }
    }
}

现在:让我们以以下形式进行测试:

Now: let''s test in a Form:

// assume you have created a WinForms Project
// and that it has the standard default Form, Form1
//
// requires at least these libraries:
using System;
using System.Windows.Forms;
using System.Threading;
//
namespace TestTimerInClass_Form
{
    public partial class Form1
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            // note because we used a separate NameSpace
            // the fully qualified name of the class, which must include
            // the NameSpace must be used
            ClassWithTimer.TimerInClass InstanceOfTimer = new ClassWithTimer.TimerInClass(10, false);
        
            InstanceOfTimer.StartTimer();
        
            for(int x = 0; x < 100; x++)
            {
                // let's waste some time
                Thread.Sleep(10);
            }
        
            InstanceOfTimer.StopTimer();
        }
    }
}

运行此代码,然后在Visual Studio的输出"窗口中检查结果,以验证Timer是否被重复调用.

...编辑#1 ... 2012年2月18日...添加了Form测试台的NameSpace的括号和``Form1部分括号",因此使用Timer类的全限定名的原因在另一个NameSpace中更清晰,希望该代码对于新用户"更具可读性.

...编辑#2 ... 2012年2月18日...添加了设置'System.Timer tp的AutoReset属性'true ...,尽管这不是必需的,因为它的默认值为正确:只是认为这可能会增加答案的教育价值".

Run this code, and examine the results in the Output Window in Visual Studio to verify the Timer is being called repeatedly.

... edit #1 ... Feb. 18, 2012 ... added the bracketing of the NameSpace of the Form test-bed, and ''partial Form1 bracketing, so the reason for using the fully qualified name of the Timer class in another NameSpace is more clear, and the code is hopefully more readable to a "new user."

... edit #2 ... Feb. 18, 2012 ... added setting the ''AutoReset Property of the System.Timer tp ''true ... even though this is not necessary since it''s default value is true: just thought it might increase the "educational value" of the answer.


类似的东西?
something like this?
using System;
using System.Windows.Forms;

namespace TestCSharp
{
    public class MyClass
    {
        Timer _myTimer;


        public void New()
        {
            _myTimer = new Timer();
            _myTimer.Interval = 1000;
            _myTimer.Tick += _myTimer_Tick;
            _myTimer.Start();
        }

        void _myTimer_Tick(object sender, EventArgs e)
        {
            // Do something
        }
    }
}


这篇关于在类文件中使用计时器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆