本文介绍了定时器便携式图书馆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到便携式库/ Windows应用商店的一个计时器。 (靶向.NET 4.5和Windows商店又名地铁)

I can't find a timer in portable library / Windows Store. (Targeting .net 4.5 and Windows Store aka Metro)

是否有对如何创建某种定时事件的想法?

Does any have an idea on how to created some kind of timing event?

我需要一个秒表somekind的,所以这应该refreshn一次一秒钟左右。

I need somekind of a stopwatch, so this should refreshn once a second or so

推荐答案

更新::我们已经在Visual Studio中修复了这个2013年便携式图书馆面向存储(Windows 8.1中)和.NET Framework 4.5.1项目现在可以参考定时器。

Update: We have fixed this in Visual Studio 2013. Portable libraries targeting Store (Windows 8.1) and .NET Framework 4.5.1 projects can now reference Timer.

这就是我们的实施细节泄露给用户的不幸情况。当你的目标只是.NET 4.5和Windows Store应用程序,我们实际上会导致你建立针对不同的东西那么当你针对一个低级别的平台(.NET 4,SL 4/5,电话7.x版)。我们试图把这两一样的,但有限的变化下开始泄漏(如定时器和反射)。我们介绍一些这个位置:http://channel9.msdn.com/Shows/Going+Deep/NET-45-David-Kean-and-Marcea-Trofin-Portable-Libraries.

This is unfortunate case of where our implementation details are leaking to the user. When you target just .NET 4.5 and Windows Store apps, we actually cause you to build against something different then when you target a down-level platform (.NET 4, SL 4/5, Phone 7.x). We try treat these two as the same, but limited changes underneath start to leak (such as Timer, and Reflection). We cover some of this here: http://channel9.msdn.com/Shows/Going+Deep/NET-45-David-Kean-and-Marcea-Trofin-Portable-Libraries.

我们将看看在未来版本中修复此。在那之前,你有几个解决方法:

We'll look at fixing this in a future version. Until then, you have a couple of workarounds:

1)用C语言实现Task.Delay的定时器自己的版本,这里是我们正在内部使用快速复制:

1) Implement your own version of Timer using Task.Delay, here's a quick copy that we're using internally:

internal delegate void TimerCallback(object state);

internal sealed class Timer : CancellationTokenSource, IDisposable
{
    internal Timer(TimerCallback callback, object state, int dueTime, int period)
    {
        Contract.Assert(period == -1, "This stub implementation only supports dueTime.");
        Task.Delay(dueTime, Token).ContinueWith((t, s) =>
        {
            var tuple = (Tuple<TimerCallback, object>)s;
            tuple.Item1(tuple.Item2);
        }, Tuple.Create(callback, state), CancellationToken.None,
            TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion,
            TaskScheduler.Default);
    }

    public new void Dispose() { base.Cancel(); }
}

2)降级您的项目以.NET 4.0和Windows Store应用程序,这将让您使用定时器。

2) Downgrade your project to .NET 4.0 and Windows Store apps, which will give you access to Timer.

3)创建一个新的项目面向.NET 4.0和Windows Store应用程序,并把code,需要定时在。然后引用从.NET 4.5和Windows Store应用程序的项目。

3) Create a new project targeting .NET 4.0 and Windows Store apps, and put the code that requires timer in that. Then reference that from the .NET 4.5 and Windows Store apps project.

作为一个方面说明,我在提交了工作项目为自己的PclContrib网站添加定时器支持:<一href=\"http://pclcontrib.$c$cplex.com/workitem/12513\">http://pclcontrib.$c$cplex.com/workitem/12513.

As a side note, I've filed a work item for myself over on PclContrib site to add Timer support: http://pclcontrib.codeplex.com/workitem/12513.

这篇关于定时器便携式图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 21:19