C#计时器组件的创建 [英] c# timer component creation

查看:62
本文介绍了C#计时器组件的创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#还是很陌生,当我将以下代码添加到Windows应用程序中时,我在组件类中使用了以下代码,并调用了start函数,计时器没有消失并且计数值未显示任何人都可以清除的问题它.


i am very new to C# i used the following code for my component class when i add this is to my windows application and calling start function the timer is not elapsing and the count value is not displaying what may the problem can anyone clear it.


using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.ComponentModel;
namespace timerer
{
    public class timercomp:Component
    {
        public int count=0;

        System.Timers.Timer myTimer = new System.Timers.Timer();

        public timercomp(IContainer container)
        {
            container.Add(this);
             }
             public int start()
             {

                myTimer.Enabled = true;
                 myTimer.Interval += 1000;
                 myTimer.Elapsed += new ElapsedEventHandler(OnTimer);
                myTimer.AutoReset = true;
                 return count;
             }
        public void OnTimer(Object source, ElapsedEventArgs e)
        {
            myTimer.Enabled = false;
            myTimer.Interval += 1000;
            count = count + 1;
            start();

        }

    }
}

推荐答案

如果要使用计时器,则应仅使用现有的计时器之一(System.Windows.Forms.Timer或System.Threading.计时器).您的组件没有意义,完全没有必要.
If you want a timer, you should just use one of the existing timers (System.Windows.Forms.Timer or System.Threading.Timer). Your component doesn''t make sense and is completely unnecessary.


using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.ComponentModel;
namespace timerer
{
    public class timercomp:Component
    {
        public int count=0;
 
        System.Timers.Timer myTimer = new System.Timers.Timer();
 
        public timercomp(IContainer container)
        {
            container.Add(this);
            myTimer.Elapsed += new ElapsedEventHandler(OnTimer);
        }
        
        public int start()
        {
 
                myTimer.Enabled = true;
                 myTimer.Interval += 1000;
                 myTimer.AutoReset = true;
                 return count;
        }
        
        public void OnTimer(Object source, ElapsedEventArgs e)
        {
            myTimer.Enabled = false;
            myTimer.Interval += 1000;
            count = count + 1;
            start();
 
        }
 
    }
}

//somewhere in your code you need
timercomp comp = new timercomp();
timercomp.start();



不知道为什么要在开始时返回count,当在上面的代码之外调用count时,它总是返回0.不过,应该使用调试器看到它​​递增.
请注意,您的间隔越来越大,不知道这是否是您的意图.



don''t know why you return count in start, when it is called outside the code above it will always return 0. You should see it incrementing using the debugger though.
Note your intervals are getting bigger and bigger, don''t know if that was your intention.


这篇关于C#计时器组件的创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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