01. 多线程Task做延迟操作

1.异步执行,不会阻塞当前线程

public CompassCalibrationTopIndicatorView()
        {
            InitializeComponent();
            BindingContext = Ioc.ViewModelLocator.GetViewModel();

            Task.Run(async () =>
            {
                await Task.Delay(3000);
                Device.BeginInvokeOnMainThread(() =>
                {
                    Button1.Status = EHButtonStatus.Selected;
                });
            });
        }

2. 同步等待,当前线程会等三秒完后继续往下执行

            var task = Task.Delay(3000);
            task.Wait();
            Button1.Status = EHButtonStatus.Selected;

3. 异步,不阻塞当前线程的执行,

Task.Delay(3000).ContinueWith((b) =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            TriangleImage.TranslateTo(viewModel.UiModel.IndicatorWidth * 0.5, 0);
                        });
                    });

4. await实现延迟操作

 protected override void OnAppearing()
        {
            base.OnAppearing();

            HidePopup();
        }

        private async void HidePopup()
        {
            await Task.Delay(4000);
            await PopupNavigation.RemovePageAsync(this);
        }

连续嵌套延迟

 Task.Delay(2000).ContinueWith(_ =>
            {
               //do something
            }).ContinueWith(async _ =>
            {
                await Task.Delay(2000);
               //do something

            });

你可能感兴趣的:(01. 多线程Task做延迟操作)