如何在 Compact Framework 上处置 Forms.Timer
在 Compact Framework 上,System.Windows.Forms.Timer 类不支持 system.componentmodel 构造函数:
支持 new Timer(): http://msdn.microsoft.com/en-us/library/aa335543(v=vs.71).aspx
新不支持计时器(IContainer 容器): http:// /msdn.microsoft.com/en-us/library/aa335544(v=vs.71).aspx
这意味着,当我将计时器添加到 CF 应用程序中的表单时,它不会添加到表单的 IContainer 组件字段,因此当表单被 dispose()d 时,它不会获得 auto-dispose()d。
为什么不支持这个?
当我的表单被处理时,我应该如何最好地处理计时器?看来我有两个主要选择:
- 将表单的 dispose() 方法从 .designer.cs 移动到我的主 .cs 文件中,并在其中添加手动“_timer.dispose()”调用
- ,或者手动将 Timer 对象添加到调用 InitializeComponent() 后,创建表单时的组件集合
我应该选择哪一个?如果我忘记执行这两项操作之一,计时器将永远存在,使我的表单保持活动状态(因为计时器无法被 GC 处理,并且它保存对我表单的 Timer_Tick() 方法的引用,因此表单永远无法被 GC 处理) )。
这个实施决策是否反映了我需要注意的关于 CF 机器上的计时器和处理的一些奇怪之处?
On the Compact Framework, the System.Windows.Forms.Timer class doesn't support the system.componentmodel constructor:
new Timer() is supported: http://msdn.microsoft.com/en-us/library/aa335543(v=vs.71).aspx
new Timer(IContainer container) is not supported: http://msdn.microsoft.com/en-us/library/aa335544(v=vs.71).aspx
This means that, when I add a Timer to a Form in a CF app, it does not get added to the form's IContainer components field, so it doesn't get auto-dispose()d when the form is dispose()d.
Why is this not supported?
How should I best dispose of Timers when my form is disposed? It seems that I have two main options:
- move the form's dispose() method from the .designer.cs into my main .cs file and add a manual "_timer.dispose()" call in there
- or manually add the Timer object to the components collection on form creation, after InitializeComponent() has been called
Which should I prefer? If I forget to do one of these two, the Timer will live forever, keeping my form alive (because the Timer can't be GCed, and it holds a reference to my form's Timer_Tick() method, so the form can never be GCed).
Does this implementation decision reflect some strangeness about Timers and Disposing on CF machines which I need to be aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会投票给选项 3:不要通过表单设计器添加计时器;相反,在代码中手动添加它,并手动将其添加到组件集合中。
我对此的推理如下:
I'd vote for option 3: don't add the Timer through the Form Designer; instead add it manually in code and add it to the components collection manually as well.
My reasoning for this follows like this: