zoukankan      html  css  js  c++  java
  • WPF 子线程不能直接修改主线程UI的界面

    次线程不能直接修改主线程UI的界面,需要使用以下方法

    this.Dispatcher.Invoke(DispatcherPriority.Normal,
    new Action(() =>
    {
      //调用主线程UI的的代码                           
    }));

    如:

    void LoadFile()
            {
                try
                {
                    if (string.IsNullOrEmpty(filename))
                    {
                        //加载建设中
                        return;
                    }
                    if (!File.Exists(filename))
                    {
                        //加载建设中
                        return;
                    }
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read))
                        {
                            string ext = System.IO.Path.GetExtension(filename);
                            System.Windows.Documents.FlowDocument flowDocument = new System.Windows.Documents.FlowDocument();
                            flowDocument.IsOptimalParagraphEnabled = true;
                            flowDocument.FlowDirection = System.Windows.FlowDirection.LeftToRight;
    
                            flowDocument.IsHyphenationEnabled = true;
    
                            switch (ext)
                            {
                                case ".rtf":
    
                                    TextRange range = new TextRange(
                                                        flowDocument.ContentStart,
                                                        flowDocument.ContentEnd);
                                    range.Load(fs, DataFormats.Rtf);
                                    break;
                                default:
                                    break;
                            }
                            flowDocument.ColumnWidth = 540;
                            FlowDocPageView1.Document = flowDocument;
                        }
    
                    }), DispatcherPriority.Normal);
    
                }
                catch { return; }
            }
  • 相关阅读:
    超级楼梯
    hdu1040
    hdu2033(惭愧)
    hdu2032杨辉三角
    hdu1013Digital Roots
    hdu2031
    Linux信号(signal) 机制分析
    android init重启service(进程)
    [android] init进程 .rc文件中service、action的parsing
    oom_adj
  • 原文地址:https://www.cnblogs.com/liusir/p/3203619.html
Copyright © 2011-2022 走看看