Fork me on GitHub

WPF多UI线程

 

 

 

 internal class SpashWindowManager
    {
        private static SpashWindow _spashWindow;
        private static Thread thread;
        public static void Show()
        {
            thread = new Thread(() =>
           {
               _spashWindow = new SpashWindow();
               _spashWindow.Show();
               try
               {
                   Dispatcher.Run();
               }
               catch (Exception e)
               {
                   LogHelper.WriteLog(e.Message, LogType.Error);
               }
           })
            { IsBackground = true };
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        public static void Close()
        {
            Dispatcher.FromThread(thread)?.Invoke(() =>
                {
                    try
                    {
                        _spashWindow.Close();

                    }
                    catch (Exception e)
                    {
                        LogHelper.WriteLog($" {e.StackTrace.ToString()}  {e.Message}", LogType.Error);
                    }
                });
        }
    }

  

posted @ 2023-11-08 10:13  黄高林  阅读(60)  评论(0编辑  收藏  举报