从新线程线程休眠而不添加延迟

时间:2018-01-31 22:42:52

标签: c# wpf multithreading

我希望显示一个类似4秒的初始启动窗口,然后进入下一个窗口,即我的情况下的NextGenDGRunSetupWindow。所以只是为了模仿那些显示Splash画面的4秒钟我开始一个新的主题并说Thread.Sleep(4000);在新线程上。但问题是当我运行应用程序时,我看不到任何东西4秒钟,并且NextGenDGRunSetupWindow窗口正在启动。如何在新线程上添加延迟并在进入下一个窗口之前显示我的启动画面。请参阅视图模型中的CheckIsInstrumentReadyToRun()方法。

这是我的ViewModel

public class SplashViewModel : INotifyPropertyChanged
    {
        private bool _showLoadingDisplay;
        public bool ShowLoadingDisplay
        {
            get
            {
                return _showLoadingDisplay;
            }
            set
            {
                _showLoadingDisplay = value;
                OnPropertyChanged("ShowLoadingDisplay");
            }
        }

        public event Action<InstrumentStateEnum> CanProceedFurtherAction;

        private static SplashViewModel _instance;
        public static SplashViewModel SplashViewModelInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SplashViewModel();
                }
                return _instance;
            }
        }
        public SplashViewModel()
        {
            ShowLoadingDisplay = true;
        }

        public void CheckIsInstrumentReadyToRun()
        {
            //JUST TO MIMIC SOMETHING IS HAPPENING STARTING A NEW THREAD AND THAT ONE WAITS
            Thread t1 = new Thread(new ThreadStart(CallFirmaware));
            t1.Start();
            var value = InstrumentStateEnum.Loading;
            t1.Join();
            //Fire the event
            CanProceedFurtherAction?.Invoke(value);
        }

        public void CallFirmaware()
        {
            Thread.Sleep(4000);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

背后的代码

public partial class SplashScreen : Window
    {
        public SplashScreen()
        {
            InitializeComponent();
            SplashViewModel splashViewModel = SplashViewModel.SplashViewModelInstance;
            this.DataContext = splashViewModel;
            splashViewModel.CanProceedFurtherAction += SplashViewModel_CanProceedFurtherAction;
            splashViewModel.CheckIsInstrumentReadyToRun();
        }

        private void SplashViewModel_CanProceedFurtherAction(BioRad.NextGenDG.Model.Shared.InstrumentStateEnum obj)
        {
            if (obj == BioRad.NextGenDG.Model.Shared.InstrumentStateEnum.Loading)
            {
                NextGenDGRunSetupWindow SetUpWindow = new NextGenDGRunSetupWindow();
                SetUpWindow.Show();
            }
            this.Close();
            //ERROR OCCURRED TAKE TO SOMEWHERE
        }
    }

0 个答案:

没有答案