zoukankan      html  css  js  c++  java
  • 通知UI线程 WPF

    使用  Process 创建新的线程

            private Process _ProcessSearch;
            private void InitializeProcess()
            {
                try
                {
                    _ProcessSearch = new Process("Search");
                    _ProcessSearch.OnStart += _ProcessSearch_OnStart;
                    _ProcessSearch.OnCompleted += _ProcessSearch_OnCompleted;
                }
                catch (PresentationException pex)
                {
                    throw pex;
                }
                catch (Exception ex)
                {
                    throw new PresentationException(ex);
                }
            }

    启动线程

    public void Search()
     {
       _ProcessSearch.Start();
     }

    在Start方法里通知UI线程

            private void _ProcessSearch_OnStart(object sender, ProcessStartedEventArgs e)
            {
                try
                {
                    Application.Current.Dispatcher.Invoke(() => YourMethod());
                }
                catch (PresentationException pex)
                {
                    pex.Report();
                }
                catch (Exception ex)
                {
                    new PresentationException(ex).Report();
                }
            }

    不会阻塞UI线程

    Application.Current.Dispatcher.Invoke(() => YourMethod());

    使用MVVM的话可以直接用

    View.Dispatcher.Invoke(() => YourMethod());
  • 相关阅读:
    蘑菇街
    康拓展开
    CSS学习笔记
    专业名词
    专业名字
    01背包问题
    将bbr功能合入到centos7.3
    How to Identify User&Password of DataBase safely in SQL statement?
    tips for private constructor
    all Key Word of C#
  • 原文地址:https://www.cnblogs.com/zqt14520/p/8638061.html
Copyright © 2011-2022 走看看