Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Thread.Sleep in Xamarin.Forms

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.

like image 412
testing Avatar asked Mar 03 '16 09:03

testing


People also ask

What is the use of thread sleep in C#?

In C#, a Sleep() method temporarily suspends the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution. There are two methods in the overload list of Thread. Sleep Method as follows: Sleep(Int32)

Is thread sleep blocking?

The problem : Thread. sleep – it blocks the thread. This makes it unusable until it resumes, preventing us from running 2 tasks concurrently. Fortunately, there are solutions, which we'll discuss next.

How do I cancel thread sleep?

Thread. Sleep cannot be cancelled. To inject a pause, consider using the Cancellation Token instead.


2 Answers

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)

like image 163
Sven-Michael Stübe Avatar answered Sep 20 '22 16:09

Sven-Michael Stübe


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();
like image 30
Rahul Tripathi Avatar answered Sep 17 '22 16:09

Rahul Tripathi