计时器在Windows 8.1 - 如何模仿定时器(TimerCallback)构造函数? [英] Timer in Windows 8.1 - how to mimic Timer(TimerCallback) constructor?
问题描述
我移植使用的 System.Threading.Timer 到Windows应用商店中Windows 8.1。在定时
类是可用的,但有几个选择似乎就缺少到相应的.NET框架定时
。
I am porting an existing .NET class library that uses System.Threading.Timer to a Windows Store app that targets Windows 8.1. The Timer
class is available, but a few options seem to be missing in relation to the corresponding .NET Framework Timer
.
在特别是有在Windows应用商店的版本只有两个构造函数:
In particular, there are only two constructors available in the Windows Store version:
public Timer(TimerCallback callback, Object state, int dueTime, int period);
public Timer(TimerCallback callback, Object state, TimeSpan dueTime, TimeSpan period);
.NET框架包含这个额外的构造函数:
.NET Framework contains this additional constructor:
public Timer(TimerCallback callback);
它根据的 MSDN文档设置 duetime参数
和周期
到 Timeout.Infinite
和状态
到定时
对象本身。
which according to the MSDN documentation sets dueTime
and period
to Timeout.Infinite
and state
to the Timer
object itself.
试图取代单一参数的构造函数,我有天真试图通过定时
对象到Windows 8.1构造函数之一,是这样的:
Trying to replace the single argument constructor, I have "naively" tried to pass the Timer
object into one of the Windows 8.1 constructors, like this:
Timer t;
t = new Timer(MyCallback, t, Timeout.Infinite, Timeout.Infinite); // WILL NOT WORK!!!
但当然,这仅产生编译错误
but of course this only yields the compilation error
未分配的局部变量'T'使用
Use of unassigned local variable 't'
也没有国家
二传手或 SETSTATE
方法在定时
类,所以在状态
不能施工后设置。
There is also no State
setter or SetState
method in the Timer
class, so the state
cannot be set after construction.
我能做些什么,以完全模仿完整的框架的定时器(TimerCallback )
构造?
What can I do to fully mimic the full framework's Timer(TimerCallback)
constructor?
推荐答案
请注意,当你手动后开始计时这些选项是可以接受的,只要现场/属性设置,这意味着使用 Timeout.Infinite
为适当的时候,你是。
Note that these options are acceptable as long as you start the timer manually after the field/property is set, which means using Timeout.Infinite
for due time which you are.
属性添加到一个状态对象:
Add a property to a state object:
public class MyState
{
public Timer { get; set; }
}
//create empty state
MyState s = new MyState();
//create timer paused
Timer t = new Timer(MyCallback, s, Timeout.Infinite, Timeout.Infinite);
//update state
s.Timer = t;
//now safe to start timer
t.Change(..)
private字段
Private Field
_t = new Timer(MyCallback, null, Timeout.Infinite, Timeout.Infinite);
MyCallback(object state)
{
// now have access to the timer _t
_t.
}
的内部类的私有字段
如果因为要启动和跟踪多个一体的民营领域是不够的,使该包装一个计时器一个新的类。这可能是一个内部类:
Private Field of an Inner Class
And if one private field is not enough because you want to launch and track multiple, make a new class that wraps a timer. This could be an inner class:
public class ExistingClass
{
public void Launch()
{
new TimerWrapper(this);
}
private sealed class TimerWrapper
{
private readonly ExistingClass _outer;
private readonly Timer _t;
public TimerWrapper(ExistingClass outer)
{
_outer = outer;
//start timer
_t = new Timer(state=>_outer.MyCallBack(this),
null, Timeout.Infinite, Timeout.Infinite);
}
public Timer Timer
{
get { return _t; }
}
}
private void MyCallBack(TimerWrapper wrapper)
{
wrapper.Timer.
}
}
这篇关于计时器在Windows 8.1 - 如何模仿定时器(TimerCallback)构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!