c# - Problems with System.Threading.Timer in PCL Profile 78 - Warnings -
i surprised convenient system.threading.timer class not exist in profile 78 libraries. use class created pcl targets 4.0 framework , wrote simple wrapper around (as suggested in 1 blog post):
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); } }
now can reference 4.0 library , use pcltimer in main pcl library. when try build main android project following warnings:
warning cs1684: reference type 'system.threading.timer' claims defined in 'c:\program files (x86)\reference assemblies\microsoft\framework\.netportable\v4.5\profile\profile78\mscorlib.dll', not found (cs1684) (prototype.core) warning msb3247: found conflicts between different versions of same dependent assembly. (msb3247) (prototype.droid)
how rid of these warnings properly?
here complete re-implementation of timer class temporarily disappeared in profile 78, using asynchronous tasks:
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; } } } }
Comments
Post a Comment