Using Thread.Sleep in Xamarin.Forms
Solution 1
If you want to wait
asynchronously: await Task.Delay(10000);
synchronously: Task.Delay(10000).Wait();
But please try to avoid blocking the UI thread to ensure a good user experience and keep your app responsive.
Using Thread.Sleep in Xamarin.Forms
There are two Xamarin.Forms templates:
Xamarin.Forms Portable Class Library
Because of the PCL subset mechanism, you have no chance to get
Thread.Sleep
.Update 2017: PCLs are deprecated, now. If you use .netstandard 2.0, you can use
Thread.Sleep
as you are used to it.Xamarin.Forms Shared Project
This Template contains different platforms that do not support
Thread.Sleep
. Windows UWP, Xamarin.Android and Xamarin.iOS support it, Windows Phone 8.1, and Windows 8.1 not. If you unload/delete the 2 projects, the solution builds. (don't forget using System.Threading; in your App.cs)
Solution 2
You can try to use
await Task.Delay(milliseconds);
if you want to put a delay in your thread.
For blocking you can do
Task.Delay(TimeSpan.FromSeconds(5)).Wait();
testing
Updated on July 09, 2022Comments
-
testing almost 2 years
I want to execute the following
MainPage = new ContentPage { Content = new StackLayout { Children = { new Button { Text = "Thread.Sleep", Command = new Command(() => { Thread.Sleep(1000); MainPage.Animate("", x => MainPage.BackgroundColor = Color.FromRgb(x, x, x)); }), }, new Button { Text = "Task.Run + Thread.Sleep", Command = new Command(async () => { await Task.Run(() => Thread.Sleep(1000)); MainPage.Animate("", x => MainPage.BackgroundColor = Color.FromRgb(x, x, x)); }) }, new Button { Text = "Device.StartTimer", Command = new Command(() => Device.StartTimer( TimeSpan.FromSeconds(1), () => { MainPage.Animate("", x => MainPage.BackgroundColor = Color.FromRgb(x, x, x)); return false; })), }, } } };
I included
System.Threading
andSystem.Threading.Tasks
, but I still getThe name 'Thread' does not exist in the current context.
This site suggests that
Thread.Sleep
can be used inXamarin.Forms
. I have this in a shared project, not a PCL. -
testing about 8 yearsYou could do that, but that is different from the example I want to try. The thread should get blocked with the first button not delayed. Do you have an idea what I'm missing? A reference in the sub projects?
-
Rahul Tripathi about 8 years@testing:- Updated my answer to block it. Please check
-
testing about 8 yearsYou are right that the UI thread should not get blocked. Here it is only for demonstration purposes.
-
testing about 8 yearsDo you have an idea why
Thread
isn't recognized? On the linked page it seems that it somehow works. -
Sven-Michael Stübe about 8 yearsI think you don't have
Thread.Sleep
because of a different PCL profile of your Core library. The more supported platforms you select in the PCL, the less supported classes / functions you can use. -
testing about 8 yearsAny ideas where this can be changed?
-
Sven-Michael Stübe about 8 yearsIf you use a
Shared Library
instead of aPortable Class Library
, it should work. But be aware, that some platforms simply don't supportThread.Sleep
(e.g. WinPhone 8.1) -
testing about 8 yearsI tried it in a new
Blank App (Xamarin.Forms Shared)
and I get the same behavior. Strange ... -
Sven-Michael Stübe about 8 yearsThis is because the Template contains different platforms that do not support
Thread.Sleep
. Windows UWP, Xamarin.Android and Xamarin.iOS support it, Windows Phone 8.1, and Windows 8.1 not. if you unload/delete the 2 projects, the solution builds. (don't forgetusing System.Threading;
in your App.cs. -
testing about 8 yearsThat is the solution. Please edit you answer and I'll give you the tick.
-
Sven-Michael Stübe about 8 yearsI summarized it in the answer.