I am building a Xamarin cross platform app targeting IOS Android and Windows Phone 8.1 using .Net 4.5.1. When I try to reference System.Timers in the PCL project, it is not there. How do I fix this?
Asked
Active
Viewed 5,155 times
6
-
This 'behavior' matches and follows what is actually available in 4.5.1 portable. I believe it is now in 4.5.2, but that does not help you now on Xamarin/iOS-Andriod. See for details: http://stackoverflow.com/questions/12555049/timer-in-portable-library – SushiHangover Jun 29 '15 at 04:27
-
1Also discussed and replied to by Xamarin on their forums: http://forums.xamarin.com/discussion/17227/timer-in-portable-class-library – SushiHangover Jun 29 '15 at 04:37
2 Answers
11
You can use : Device.StartTimer
Syntax :
public static void StartTimer (TimeSpan interval, Func<bool> callback)
Examples : increment number every 1 seconds for 1 minutes
int number = 0;
Device.StartTimer(TimeSpan.FromSeconds(1),() => {
number++;
if(number <= 60)
{
return true; //continue
}
return false ; //not continue
});
Examples : wait for 5 seconds to run function one time
Device.StartTimer(TimeSpan.FromSeconds(5),() => {
DoSomething();
return false ; //not continue
});

jojobarcream
- 589
- 4
- 8
-
2**Device.StartTimer** is just part of **Xamarin.Forms** and it is not compatible with Xamarin Native Development's **Portable class library** or **Shared library** – Durai Amuthan.H Jun 02 '17 at 15:15
5
I noticed this the other day. Eventhough the class is in the API documentation System.Threading.Timer Class..Annoying.
Anyway I created my own Timer class, using Task.Delay()
:
public class Timer
{
private int _waitTime;
public int WaitTime
{
get { return _waitTime; }
set { _waitTime = value; }
}
private bool _isRunning;
public bool IsRunning
{
get { return _isRunning; }
set { _isRunning = value; }
}
public event EventHandler Elapsed;
protected virtual void OnTimerElapsed()
{
if (Elapsed != null)
{
Elapsed(this, new EventArgs());
}
}
public Timer(int waitTime)
{
WaitTime = waitTime;
}
public async Task Start()
{
int seconds = 0;
IsRunning = true;
while (IsRunning)
{
if (seconds != 0 && seconds % WaitTime == 0)
{
OnTimerElapsed();
}
await Task.Delay(1000);
seconds++;
}
}
public void Stop()
{
IsRunning = false;
}
}

JKennedy
- 18,150
- 17
- 114
- 198
-
-
@AR89 Good spot, It could be refactored to use `WaitTime`. At the moment it cycles round every `second` `(Task.Delay(1000))` then every `seconds` compares its value to `WaitTime` to see if it needs to fire the `TimerElapsed` event – JKennedy May 18 '17 at 08:42
-
`await Task.Delay(1000); seconds++; OnTimerElapsed();` This would be a right refactor? (I'll test it, but I can't right now) – AR89 May 18 '17 at 09:20