
令我感到惊讶的是,Profile 78库中不存在方便的System.Threading.Timer类.为了使用这个类,我创建了另一个针对4.0框架的PCL,并编写了一个简单的包装器(正如一篇博客文章中所建议的那样):
public class PCLTimer{ private Timer timer; private Action<object> action; public PCLTimer (Action<object> action, object state, int dueTimeMilliseconds, int periodMilliseconds) { this.action = action; timer = new Timer (PCLTimerCallback, state, dueTimeMilliseconds, periodMilliseconds); } private voID PCLTimerCallback (object state) { action.Invoke (state); } public bool Change (int dueTimeMilliseconds, int periodMilliseconds) { return timer.Change (dueTimeMilliseconds, periodMilliseconds); }}现在我可以参考这个4.0库并在主PCL库中使用PCLTimer.但是当我尝试构建我的主要AndroID项目时,我收到以下警告:
Warning CS1684: Reference to type 'System.Threading.Timer' claims it is defined in 'c:\Program files (x86)\Reference AssemblIEs\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile78\mscorlib.dll', but it Could not be found (CS1684) (Prototype.Core)Warning MSB3247: Found conflicts between different versions of the same dependent assembly. (MSB3247) (Prototype.DroID)如何正确摆脱这些警告?
解决方法:
下面是使用异步任务暂时消失在Profile 78中的Timer类的完全重新实现:
using System;using System.Threading;using System.Threading.Tasks;namespace Quantum{ public delegate voID TimerCallback(object state); public sealed class Timer : Idisposable { private static Task CompletedTask = Task.Fromresult(false); private TimerCallback Callback; private Task Delay; private bool disposed; private int Period; private object State; private CancellationTokenSource TokenSource; public Timer(TimerCallback callback, object state, int dueTime, int period) { Callback = callback; State = state; Period = period; reset(dueTime); } public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) : this(callback, state, (int)dueTime.TotalMilliseconds, (int)period.TotalMilliseconds) { } ~Timer() { dispose(false); } public voID dispose() { dispose(true); GC.SuppressFinalize(this); } private voID dispose(bool cleanUpManagedobjects) { if (cleanUpManagedobjects) Cancel(); disposed = true; } public voID Change(int dueTime, int period) { Period = period; reset(dueTime); } public voID Change(TimeSpan dueTime, TimeSpan period) { Change((int)dueTime.TotalMilliseconds, (int)period.TotalMilliseconds); } private voID reset(int due) { Cancel(); if (due >= 0) { TokenSource = new CancellationTokenSource(); Action tick = null; tick = () => { Task.Run(() => Callback(State)); if (!disposed && Period >= 0) { if (Period > 0) Delay = Task.Delay(Period, TokenSource.Token); else Delay = CompletedTask; Delay.ContinueWith(t => tick(), TokenSource.Token); } }; if (due > 0) Delay = Task.Delay(due, TokenSource.Token); else Delay = CompletedTask; Delay.ContinueWith(t => tick(), TokenSource.Token); } } private voID Cancel() { if (TokenSource != null) { TokenSource.Cancel(); TokenSource.dispose(); TokenSource = null; } } }} 总结 以上是内存溢出为你收集整理的c# – PCL配置文件78中System.Threading.Timer的问题 – 警告全部内容,希望文章能够帮你解决c# – PCL配置文件78中System.Threading.Timer的问题 – 警告所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)