弱鸡2,C# 一个List里面都是timer,如果list被clear,timer事件还会执行吗?
来源:互联网 发布:优酷显示网络请求出错 编辑:程序博客网 时间:2024/05/16 23:51
原来是不清楚的,不知道list clear之后还会不会执行事件。
其实是会的,因为这个timer对象还是在内存中的,只是clear被移除了引用。
原猜测是不是垃圾回收之后就会没有。,但实际上执行垃圾回收也没用。
不知道.net里面是不是timer不能被回收(因为有事件一直在等待或执行啊,所以不会被回收,因此GC.Collect()也没用?)
有大侠知道的话请指点。
using System;using System.Collections.Generic;using System.Threading;using System.Timers;using Timer = System.Timers.Timer;namespace ConsoleApplication1{ class Program { static List<Timer> timerList = new List<Timer>() { }; static void Main(string[] args) { for (int i = 0; i < 5; i++) { Timer timer = new Timer(5000) { Enabled = true }; timer.Elapsed += Print; timerList.Add(timer); timer.Start(); } Thread thread = new Thread(NewThread); thread.Start(); Console.Read(); } private static void NewThread() { Console.WriteLine("进入线程,开始睡15S" + DateTime.Now.ToLocalTime()); Thread.Sleep(14000); Console.WriteLine("14S 之后,clear timers,注意看timers还执行吗" + DateTime.Now.ToLocalTime()); //timerList.Clear(); // GC.Collect(); timerList.ForEach(t=>t.Stop()); } private static void Print(object sender, ElapsedEventArgs e) { Console.WriteLine(DateTime.Now.ToLocalTime()); } }}
阅读全文
0 0
- 弱鸡2,C# 一个List里面都是timer,如果list被clear,timer事件还会执行吗?
- 手机休眠被唤醒后timer还继续执行吗
- 如果catch里面有return语句,finally里面的代码还会执行吗?
- timer事件执行
- C# WPF添加timer,实现Timer事件
- QT例子 List label timer
- java面试题20--如果catch里面有return语句,finally里面的代码还会执行吗?
- Timer为什么会一运行就执行?
- C#里面的三种Timer
- C#里面的三种Timer
- Timer事件
- 面试题之final,finally和finalize的区别以及如果catch里面有return语句,请问finally里面的代码还会执行吗?
- (c# ) timer
- C# Timer
- C# Timer
- C# Timer
- map.clear list.clear
- C#使用Timer 执行后台任务
- java web学习总结30:EL函数库
- CyclicBarrier和CountDownLacth
- vue.js学es6_安装vue.js并创建vue.项目
- “敏捷开发、迭代开发、瀑布开发”的简单介绍
- 网络五层协议与OSI七层协议
- 弱鸡2,C# 一个List里面都是timer,如果list被clear,timer事件还会执行吗?
- activiti学习--01什么是activiti
- 《Java核心技术(卷一)》读书笔记——第五章:继承
- mysql数据库的中文乱码问题解决方案
- Android studio中提示 unable to locate adb的解决方法
- 在线自动生成密钥,公钥
- 抽象类(abstract class)和接口(interface)有什么区别
- Java可变参数
- sql server 查看表的死锁和Kill 死锁进程