Using Thread.Sleep in Xamarin.Forms

28,352

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();
Share:
28,352
testing
Author by

testing

Updated on July 09, 2022

Comments

  • testing
    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 and System.Threading.Tasks, but I still get

    The name 'Thread' does not exist in the current context.

    This site suggests that Thread.Sleep can be used in Xamarin.Forms. I have this in a shared project, not a PCL.

  • testing
    testing about 8 years
    You 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
    Rahul Tripathi about 8 years
    @testing:- Updated my answer to block it. Please check
  • testing
    testing about 8 years
    You are right that the UI thread should not get blocked. Here it is only for demonstration purposes.
  • testing
    testing about 8 years
    Do you have an idea why Thread isn't recognized? On the linked page it seems that it somehow works.
  • Sven-Michael Stübe
    Sven-Michael Stübe about 8 years
    I 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
    testing about 8 years
    Any ideas where this can be changed?
  • Sven-Michael Stübe
    Sven-Michael Stübe about 8 years
    If you use a Shared Library instead of a Portable Class Library, it should work. But be aware, that some platforms simply don't support Thread.Sleep (e.g. WinPhone 8.1)
  • testing
    testing about 8 years
    I tried it in a new Blank App (Xamarin.Forms Shared) and I get the same behavior. Strange ...
  • Sven-Michael Stübe
    Sven-Michael Stübe about 8 years
    This 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 forget using System.Threading; in your App.cs.
  • testing
    testing about 8 years
    That is the solution. Please edit you answer and I'll give you the tick.
  • Sven-Michael Stübe
    Sven-Michael Stübe about 8 years
    I summarized it in the answer.