Currently my application have a textbox that will show 500 lines of live logging on the screen. Since the application have alot of heavy task running behind, so i try to improve this part for my application as much as i can. Open for any suggestion. Thanks.
WPF:
<TextBox Text="{Binding StatusMessage}" TextWrapping="Wrap" AcceptsReturn="True" />
Method 1:
private void OutputMessage(string msg)
{
//Result
//1000 Lines 108ms
//10000 Lines 1687ms
if(!string.IsNullOrEmpty(msg))
{
string msgWithTimeStamp = System.DateTime.Now.ToString() + ": \t" + msg;
int msgLine = 0;
if (!string.IsNullOrEmpty(StatusMessage))
{
//Only show 500 lines of latest messages...
msgLine = StatusMessage.Count(m => m == '\n');
if (msgLine > 500)
StatusMessage = StatusMessage.Remove(0, StatusMessage.IndexOf("\n") + 1);
StatusMessage = StatusMessage + "\n" + msgWithTimeStamp;
}
else
StatusMessage = msgWithTimeStamp;
}
}
Method 2 (Better performance):
List<string> msgList;
private void OutputMessage(string msg)
{
//Result
//1000 Lines 31ms
//10000 Lines 377ms
if (!string.IsNullOrEmpty(msg))
{
string msgWithTimeStamp = System.DateTime.Now.ToString() + ": \t" + msg;
msgList.Add(msgWithTimeStamp);
if (msgList.Count > 500)
msgList.RemoveAt(0);
StatusMessage = String.Join("\n", msgList);
}
}