【发布时间】:2021-11-08 12:25:39
【问题描述】:
我不知道为什么,但它总是输入两个新行:
private void getMyIPAddress()
{
String Address = "";
this.Dispatcher.Invoke(() =>
{
this.RichTextBox_logs.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
});
while (true)
{
this.Dispatcher.Invoke(() =>
{
this.RichTextBox_logs.AppendText(Environment.NewLine);
//this.RichTextBox_logs.ScrollToEnd();
});
WebRequest request = WebRequest.Create("http://checkip.dyndns.com/");
try
{
using (WebResponse response = request.GetResponse())
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
Address = stream.ReadToEnd();
}
int first = Address.IndexOf("Address: ") + 9;
int last = Address.IndexOf("</body>");
if (CurrentAddressHolder != Address.Substring(first, last - first))
{
CurrentAddressHolder = Address.Substring(first, last - first);
this.Dispatcher.Invoke(() =>
{
this.textBox_ip.Text = CurrentAddressHolder;
});
}
this.Dispatcher.Invoke(() =>
{
this.RichTextBox_logs.AppendText("IP is " + CurrentAddressHolder);
});
}
}
catch(Exception e)
{
this.Dispatcher.Invoke(() =>
{
this.RichTextBox_logs.AppendText(e.ToString());
});
}
}
}
我是多线程新手,我不确定它是否会影响新行的语法。
我们非常欢迎任何输入或代码修订/改进。谢谢。
【问题讨论】:
-
请描述一下“总是输入两个新行”。因为在所附的屏幕截图上一直只添加一行 -
IP is 49.145.41.47。 -
如果真的每次都加了两个return,你有没有试过把调试日志放在
this.RichTextBox_logs.AppendText(Environment.NewLine);和this.RichTextBox_logs.AppendText("IP is " + CurrentAddressHolder);后面?
标签: c# wpf multithreading richtextbox