在之前的问题中,我询问了如何在类似于文本框的WPF元素中获取实时日志输出(WPF append text blocks UI thread heavily but WinForms doesn't?)。那里的答案指导我使用
如上所述,我最近将我的应用程序从Windows Forms切换到了WPF。我非常喜欢WPF——它给了我在Forms中没有的许多优势。然而,性能似乎对我来说是一个相当大的问题。在我的应用程序的Forms版本中,我可以将大量文本打印到
因此,我的问题是:如何提高我的
以下是我的打印代码,供参考:
我在打印“重要”文本时会切换字号并加粗。这段代码有很多行是因为我想重用同一段落来处理所有文本,直到遇到“重要”文本; 我添加了一个包含所有“重要”文本的新段落,然后在切换回非重要文本时添加另一个段落,并将其附加到该段落中,直到再次遇到“重要”文本。我希望重用同一段落能提高性能。
另外,需要注意的是,我将stdout打印到一个FlowDocumentScrollViewer,将stderr打印到另一个FlowDocumentScrollViewer,并同时将它们打印到第三个FlowDocumentScrollViewer。因此,每行stdout和stderr技术上都会被打印两次,使我的应用程序负载加倍。再次说明,在WinForms中这不是问题。
以下是完整的代码示例,如评论中所请求的。它非常简单(3个FlowDocumentScrollViewer和简单的打印),但在约20000行文本左右就会严重减缓,而且过去更糟。
FlowDocumentScrollViewer
,确实比RichTextBox
快得多。然而,我发现运行具有大量文本输出(例如'svn co')的命令会导致我的WPF应用程序明显减速。在检出3或4个非常大的svn分支后切换选项卡需要3-4秒,并且我确定随着我进行的检出数量增加,时间会增加。滚动也有明显的延迟。如上所述,我最近将我的应用程序从Windows Forms切换到了WPF。我非常喜欢WPF——它给了我在Forms中没有的许多优势。然而,性能似乎对我来说是一个相当大的问题。在我的应用程序的Forms版本中,我可以将大量文本打印到
RichTextBox
控件中,并且我的应用程序没有任何减速。切换选项卡是瞬间完成的,滚动是无缝的。这就是我想要在我的WPF应用程序中体验的。因此,我的问题是:如何提高我的
FlowDocumentScrollViewer
的性能,以匹配Windows Forms的RichTextBox
的性能,而不会失去粗体和斜体等格式功能,并且不会失去复制/粘贴功能?只要它们提供我正在寻找的格式功能,我愿意切换WPF控件。以下是我的打印代码,供参考:
public void PrintOutput(String s)
{
if (outputParagraph.FontSize != defaultFontSize)
{
outputParagraph = new Paragraph();
outputParagraph.Margin = new Thickness(0);
outputParagraph.FontFamily = font;
outputParagraph.FontSize = defaultFontSize;
outputParagraph.TextAlignment = TextAlignment.Left;
OutputBox.Document.Blocks.Add(outputParagraph);
}
outputParagraph.Inlines.Add(s);
if (!clearOutputButton.IsEnabled) clearOutputButton.IsEnabled = true;
}
public void PrintImportantOutput(String s)
{
if (outputParagraph.FontSize != importantFontSize)
{
outputParagraph = new Paragraph();
outputParagraph.Margin = new Thickness(0);
outputParagraph.FontFamily = font;
outputParagraph.FontSize = importantFontSize;
outputParagraph.TextAlignment = TextAlignment.Left;
OutputBox.Document.Blocks.Add(outputParagraph);
}
String timestamp = DateTime.Now.ToString("[hh:mm.ss] ");
String toPrint = timestamp + s;
outputParagraph.Inlines.Add(new Bold(new Run(toPrint)));
if (!clearOutputButton.IsEnabled) clearOutputButton.IsEnabled = true;
}
我在打印“重要”文本时会切换字号并加粗。这段代码有很多行是因为我想重用同一段落来处理所有文本,直到遇到“重要”文本; 我添加了一个包含所有“重要”文本的新段落,然后在切换回非重要文本时添加另一个段落,并将其附加到该段落中,直到再次遇到“重要”文本。我希望重用同一段落能提高性能。
另外,需要注意的是,我将stdout打印到一个FlowDocumentScrollViewer,将stderr打印到另一个FlowDocumentScrollViewer,并同时将它们打印到第三个FlowDocumentScrollViewer。因此,每行stdout和stderr技术上都会被打印两次,使我的应用程序负载加倍。再次说明,在WinForms中这不是问题。
以下是完整的代码示例,如评论中所请求的。它非常简单(3个FlowDocumentScrollViewer和简单的打印),但在约20000行文本左右就会严重减缓,而且过去更糟。
编辑:代码示例已被移除。在其位置是解决我的性能问题的工作代码。它像FlowDocumentScrollViewer一样工作,只有一个例外:您无法选择行的子字符串。我正在考虑修复它,尽管这似乎很困难。
Bridge.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Threading;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace PerformanceTest
{
public class Bridge
{
int counterLimit;
public BlockingCollection<PrintInfo> output;
public BlockingCollection<PrintInfo> errors;
public BlockingCollection<PrintInfo> logs;
protected static Bridge controller = new Bridge();
public static Bridge Controller
{
get
{
return controller;
}
}
public MainWindow Window
{
set
{
if (value != null)
{
output = value.outputProducer;
errors = value.errorProducer;
logs = value.logsProducer;
}
}
}
public bool Running
{
get;
set;
}
private Bridge()
{
//20000 lines seems to slow down tabbing enough to prove my point.
//increase this number to get even worse results.
counterLimit = 40000;
}
public void PrintLotsOfText()
{
new Thread(new ThreadStart(GenerateOutput)).Start();
new Thread(new ThreadStart(GenerateError)).Start();
}
private void GenerateOutput()
{
//There is tons of output text, so print super fast if possible.
int counter = 1;
while (Running && counter < counterLimit)
{
if (counter % 10 == 0)
PrintImportantOutput("I will never say this horrible word again as long I live. This is confession #" + counter++ + ".");
else
PrintOutput("I will never say this horrible word again as long I live. This is confession #" + counter++ + ".");
//Task.Delay(1).Wait();
}
Console.WriteLine("GenerateOutput thread should end now...");
}
private void GenerateError()
{
int counter = 1;
while (Running && counter < counterLimit)
{
PrintError("I will never forgive your errors as long I live. This is confession #" + counter++ + ".");
//Task.Delay(1).Wait();
}
Console.WriteLine("GenerateError thread should end now...");
}
#region Printing
delegate void StringArgDelegate(String s);
delegate void InlineArgDelegate(Inline inline);
public void PrintOutput(String s)
{
output.TryAdd(new PrintInfo(s, false));
PrintLog("d " + s);
}
public void PrintImportantOutput(String s)
{
output.TryAdd(new PrintInfo(s, true));
PrintLog("D " + s);
}
public void PrintError(String s)
{
errors.TryAdd(new PrintInfo(s, false));
PrintLog("e " + s);
}
public void PrintImportantError(String s)
{
errors.TryAdd(new PrintInfo(s, true));
PrintLog("E " + s);
}
public void PrintLog(String s)
{
logs.TryAdd(new PrintInfo(s, false));
}
#endregion
}
public class PrintInfo
{
public String Text { get; set; }
public bool IsImportant { get; set; }
public PrintInfo() { }
public PrintInfo(String text, bool important)
{
Text = text;
IsImportant = important;
}
}
}
MainWindow.xaml
<Window x:Class="PerformanceTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
xmlns:l="clr-namespace:PerformanceTest"
WindowStartupLocation="CenterScreen">
<Grid>
<TabControl>
<TabControl.Resources>
<Style TargetType="ListBox">
<Setter Property="TextElement.FontFamily" Value="Consolas" />
<Setter Property="TextElement.FontSize" Value="12" />
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True" />
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling" />
<Setter Property="l:ListBoxSelector.Enabled" Value="True" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Command="Copy" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<TextBlock Text="{Binding Text}" TextWrapping="Wrap"
Background="{TemplateBinding Background}"
Foreground="{TemplateBinding Foreground}"
FontWeight="{TemplateBinding FontWeight}" />
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsImportant}" Value="true">
<Setter Property="TextElement.FontWeight" Value="SemiBold" />
<Setter Property="TextElement.FontSize" Value="14" />
</DataTrigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Header="Bridge">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Button Content="Start Test" Click="StartButton_Click" />
<Button Content="End Test" Click="EndButton_Click" />
</StackPanel>
</TabItem>
<TabItem Header="Output">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--<RichTextBox x:Name="OutputBox" ScrollViewer.VerticalScrollBarVisibility="Auto"/>-->
<ListBox Grid.Column="0" ItemsSource="{Binding Output,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.CommandBindings>
<CommandBinding Command="Copy" Executed="CopyExecuted" />
</ListBox.CommandBindings>
</ListBox>
<GridSplitter Grid.Column="1" Width="5" ResizeBehavior="PreviousAndNext" />
<ListBox Grid.Column="2" ItemsSource="{Binding Errors,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.CommandBindings>
<CommandBinding Command="Copy" Executed="CopyExecuted" />
</ListBox.CommandBindings>
</ListBox>
</Grid>
</TabItem>
<TabItem Header="Log">
<Grid>
<ListBox Grid.Column="0" ItemsSource="{Binding Logs,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.CommandBindings>
<CommandBinding Command="Copy" Executed="CopyExecuted" />
</ListBox.CommandBindings>
</ListBox>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
namespace PerformanceTest
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public BlockingCollection<PrintInfo> outputProducer = new BlockingCollection<PrintInfo>();
public BlockingCollection<PrintInfo> errorProducer = new BlockingCollection<PrintInfo>();
public BlockingCollection<PrintInfo> logsProducer = new BlockingCollection<PrintInfo>();
public ObservableCollection<PrintInfo> Output { get; set; }
public ObservableCollection<PrintInfo> Errors { get; set; }
public ObservableCollection<PrintInfo> Logs { get; set; }
protected FontFamily font = new FontFamily("Consolas");
protected int defaultFontSize = 12;
protected int importantFontSize = 14;
Dispatcher dispatcher;
public MainWindow()
{
Bridge.Controller.Window = this;
try
{
InitializeComponent();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.ToString());
Console.WriteLine(ex.StackTrace);
}
dispatcher = Dispatcher;
Output = new ObservableCollection<PrintInfo>();
Errors = new ObservableCollection<PrintInfo>();
Logs = new ObservableCollection<PrintInfo>();
new Thread(new ThreadStart(() => Print(outputProducer, Output))).Start();
new Thread(new ThreadStart(() => Print(errorProducer, Errors))).Start();
new Thread(new ThreadStart(() => Print(logsProducer, Logs))).Start();
}
public delegate void EmptyDelegate();
public void Print(BlockingCollection<PrintInfo> producer, ObservableCollection<PrintInfo> target)
{
try
{
foreach (var info in producer.GetConsumingEnumerable())
{
dispatcher.Invoke(new EmptyDelegate(() =>
{
if (info.IsImportant)
{
String timestamp = DateTime.Now.ToString("[hh:mm.ss] ");
String toPrint = timestamp + info.Text;
info.Text = toPrint;
}
target.Add(info);
}), DispatcherPriority.Background);
}
}
catch (TaskCanceledException)
{
//window closing before print finish
}
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
if (!Bridge.Controller.Running)
{
Bridge.Controller.Running = true;
Bridge.Controller.PrintLotsOfText();
}
}
private void EndButton_Click(object sender, RoutedEventArgs e)
{
Bridge.Controller.Running = false;
}
private void CopyExecuted(object sender, ExecutedRoutedEventArgs e)
{
ListBox box = sender as ListBox;
HashSet<PrintInfo> allItems = new HashSet<PrintInfo>(box.Items.OfType<PrintInfo>());
HashSet<PrintInfo> selectedItems = new HashSet<PrintInfo>(box.SelectedItems.OfType<PrintInfo>());
IEnumerable<PrintInfo> sortedItems = allItems.Where(i => selectedItems.Contains(i));
IEnumerable<String> copyItems = from i in sortedItems select i.Text;
string log = string.Join("\r\n", copyItems);
Clipboard.SetText(log);
}
}
}
ListBoxSelector.cs在@pushpraj的回答中。