Timer加个成员变量:执行次数

6lilu9 2018-01-06 01:44:53
想达到如题的效果,调用Timer时用两个参数(时间间隔,执行次数)。网上说可以给Timer控件加个成员变量Num,代表执行次数,可惜我还学到那个地方,简单看了看派生类的教程,也实在搞不了,于是自已琢磨了下面这个方法,但显然很不成功,请高手解疑。

一、这是我的代码,不知道什么原因不执行OnTimedEvent
using System;
using System.Timers;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MultDeskRef(1000, 3);
}

public static void MultDeskRef(int timeInterval, int Count)
{
System.Timers.Timer theTimer = new System.Timers.Timer();//定时器
theTimer.Interval = timeInterval;
theTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);//到达时间的时候执行事件;
theTimer.AutoReset = false;//设置是执行一次(false)还是一直执行(true);
theTimer.Enabled = true;
if (Num <= Count)
{ theTimer.Enabled = true; }//是否执行System.Timers.Timer.Elapsed事件;

else
{ theTimer.Enabled = false; }//是否执行System.Timers.Timer.Elapsed事件;

}

public static int Num;
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Num += 1;
Console.WriteLine("这是第{0}次运行",Num) ;
}

}
}

二、哪位高手能告知一下派生类怎么实现这个呀?
...全文
422 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2018-01-06
  • 打赏
  • 举报
回复
static不能继承,派生类直接从基类.变量名 访问
Anonymous477 2018-01-06
  • 打赏
  • 举报
回复
引用 4 楼 lengyeshuang 的回复:
TO StratosBlue: 我改成下列语句,顺利执行了。
 Console.WriteLine("这是第{0}次运行", Interlocked.Increment(ref _count));
谢谢你打了这么多,我也学到了很多(虽然还没完全懂)。
可能是你的vs版本不支持这个语法吧,我用2017是正常的
6lilu9 2018-01-06
  • 打赏
  • 举报
回复
TO StratosBlue: 我改成下列语句,顺利执行了。
 Console.WriteLine("这是第{0}次运行", Interlocked.Increment(ref _count));
谢谢你打了这么多,我也学到了很多(虽然还没完全懂)。
6lilu9 2018-01-06
  • 打赏
  • 举报
回复
引用 2 楼 StratosBlue 的回复:
你的

Console.WriteLine("这是第{0}次运行",Num) ;
的括号不是英文括号;

static void Main(string[] args)
       ......
        }
我不懂,这句是不是有错误呀,提示“$是意外字符“,而且输出结果为 "这是第{Interlocked.Increment(ref _count)}次运行 停止Timer 执行timer操作"

Console.WriteLine($"这是第{Interlocked.Increment(ref _count)}次运行")
Anonymous477 2018-01-06
  • 打赏
  • 举报
回复
你的

Console.WriteLine("这是第{0}次运行",Num) ;
的括号不是英文括号;

static void Main(string[] args)
        {
            TimerS timerS = new TimerS();
            timerS.Elapsed += TimerS_Elapsed;
            timerS.Run(500, 10);

            Console.ReadLine();
        }

        private static void TimerS_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("执行timer操作");
        }

        public class TimerS : System.Timers.Timer
        {
            /// <summary>
            /// 不允许外部更改状态
            /// </summary>
            public new bool Enabled
            {
                get
                {
                    return base.Enabled;
                }
                private set
                {
                    base.Enabled = value;
                }
            }


            private int _count;

            /// <summary>
            /// 执行计数
            /// </summary>
            public int Count
            {
                get { return _count; }
            }

            private int _num;

            /// <summary>
            /// 还需要执行的次数
            /// </summary>
            public int Num
            {
                get { return _num; }
                private set { _num = value; }
            }

            public TimerS()
            {
                Elapsed += TimerElapsed;
            }

            public void Run(int interval, int num)
            {
                if (interval <= 0 || num <= 0)
                {
                    throw new ArgumentException();
                }
                Interval = interval;
                Num = num;
                Enabled = true;
            }

            private void TimerElapsed(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine($"这是第{Interlocked.Increment(ref _count)}次运行");

                if (Interlocked.Decrement(ref _num) <= 0)
                {
                    Enabled = false;
                    Console.WriteLine("停止Timer");
                }
            }

            protected override void Dispose(bool disposing)
            {
                Elapsed -= TimerElapsed;
                base.Dispose(disposing);
            }
        }
6lilu9 2018-01-06
  • 打赏
  • 举报
回复
纠正一下:
using System;
using System.Timers;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MultDeskRef(1000, 3);
        }
               
        public static void MultDeskRef(int timeInterval, int Count)
        {
            System.Timers.Timer theTimer = new System.Timers.Timer();//定时器
            theTimer.Interval = timeInterval;
            theTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);//到达时间的时候执行事件;
            theTimer.AutoReset = false;//设置是执行一次(false)还是一直执行(true);  
            theTimer.Enabled = true;
            if (Num <= Count)
            { theTimer.Enabled = true; }//是否执行System.Timers.Timer.Elapsed事件;

            else
            { theTimer.Enabled = false; }//是否执行System.Timers.Timer.Elapsed事件;

        }

        public static int Num;
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            Num += 1;
            Console.WriteLine("这是第{0}次运行",Num) ;
        } 

    }
}

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧