• 在后台线程中,从文件一行行异步加载内容进RichTextBox


    XAML文件

    XAML文件Code
    <Grid>
        <!--一个RichTextBox读取文件中的内容-->
        <RichTextBox x:Name="LoadFileBox" Visibility="Collapsed"></RichTextBox>
        <TextBlock x:Name="CountBox" Text="" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="60"/>
        <TextBlock x:Name="TotalBox" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="30"/>
    </Grid>
    
    C#代码
    C#代码
            public MainWindow()
            {
                InitializeComponent();
                ComputeTotal();
                Task.Factory.StartNew(() =>
                {//开辟一个后台线程
                    LoadFileAsync();
                });
                
            }
            /// <summary>
            /// 计算总行数
            /// </summary>
            private void ComputeTotal()
            {
                int count = 0;
                using (var fs = new FileStream(@"D:\work\记事本项目WPF\1.txt", FileMode.Open))
                using (var reader = new StreamReader(fs))
                {
                    while (reader.Peek() > 0)
                    {
                        string line = reader.ReadLine();//异步读
                        if (line != null)
                        {
                            count++;
                        }
                    }
                    TotalBox.Text = "总数:" + count;
                }
            }
            /// <summary>
            /// 异步加载文件内容到RichTextBox
            /// </summary>
            async void LoadFileAsync()
            {
                int count = 0;
                using (var fs = new FileStream(@"D:\work\记事本项目WPF\1.txt", FileMode.Open))
                using (var reader = new StreamReader(fs))
                {
                    while (reader.Peek() > 0)
                    {
                        string line = await reader.ReadLineAsync();//异步读
                        if (line != null)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                count++;
                                CountBox.Text = "当前解析到: " + count;
                                LoadFileBox.Document.Blocks.Add(new Paragraph(new Run(line)));
                            }));
                        }
                    }
                }
                Dispatcher.Invoke(() =>
                {
                    CountBox.Visibility = Visibility.Collapsed;
                    TotalBox.Visibility = Visibility.Collapsed;
                    LoadFileBox.Visibility = Visibility.Visible;
                });
            }
    
  • 相关阅读:
    百度之星资格赛1001——找规律——大搬家
    HDU1025——LIS——Constructing Roads In JGShining's Kingdom
    DP(递归打印路径) UVA 662 Fast Food
    递推DP UVA 607 Scheduling Lectures
    递推DP UVA 590 Always on the run
    递推DP UVA 473 Raucous Rockers
    博弈 HDOJ 4371 Alice and Bob
    DFS(深度) hihoCoder挑战赛14 B 赛车
    Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
    DP(DAG) UVA 437 The Tower of Babylon
  • 原文地址:https://www.cnblogs.com/luna2333/p/15563301.html
Copyright © 2020-2023  润新知