C#使用多线程写入richtextBox
C# use multithread to write to richtextBox
我正在编写一个 C# 脚本来在网络上进行 ping 测试。除了要写入 richtextbox 的部分,我大部分 都在工作。我需要在进行 ping 测试时写下结果。我知道它需要多线程,但过去一周我一直在努力弄清楚,但做不到。这是我的代码,减去不必要的部分。我遇到麻烦的部分是 RunPingTest 函数。我还将包括我的 IP 地址示例 xml。所有按钮和其他功能都按预期工作。
using System.Diagnostics;
using System.Windows;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System;
using System.Threading;
namespace NetTestingTool
{
public partial class NetTest : Window
{
static string dataFolder = "D:";
static readonly string configPath = dataFolder + "\ConfigFiles";
//get HostIPAddresses.xml
XElement GroupNames = XElement.Load(configPath + "\HostIPAddresses.xml");
bool testIsRunning = false;
//Start the initial GUI
public NetTest()
{
InitializeComponent();
//populate the dropdown
foreach (var groupElement in GroupNames.Elements("Group"))
{
if (groupElement.Attribute("name").Value != null)
{
dropDown.Items.Add(groupElement.Attribute("name").Value);
}
}
}
//********** Button Actions **********
//run the network test
public void BtnStart_Click(object sender, RoutedEventArgs e)
{
//clear richtextbox first
resultsBox.Document.Blocks.Clear();
//first, make sure the user has selected a group
if (dropDown.SelectedItem != null)
{
string HostSelection = (string)dropDown.SelectedItem;
//create a list of IP addresses based on group chosen
List<string> pingList = new List<string>();
foreach (var groupElement in GroupNames.Elements("Group"))
{
//resultsBox.AppendText(groupElement.Attribute("name").Value);
if (groupElement.Attribute("name").Value == HostSelection)
{
// resultsBox.AppendText(groupElement);
foreach (var hostSystem in groupElement.Elements("HostSystem"))
{
if (hostSystem.Attribute("IPAddress").Value != null)
{
pingList.Add(hostSystem.Attribute("IPAddress").Value);
}
}
}
}
/*Should this be outside BtnStart_Click?
currently cannot because it uses pingList*/
void RunPingTest(object? obj)
{
//do the ping test
resultsBox.AppendText("Doing the ping test \r"); //Just kidding, it isn't
foreach (var pingElement in pingList)
{
//this needs to be able to write to resultsBox as the ping happens (richtextbox)
Ping myPing = new Ping();
resultsBox.AppendText("Pinging " + pingElement + ". . . ");
PingReply reply = myPing.Send(pingElement, 2000); //ip and timeout
string results = reply.Status.ToString();
resultsBox.AppendText(results + "\r");
}
resultsBox.AppendText("\r\n Finished ping testing");
}
}
}
//other buttons here
}
}
xml IP 地址:
<?xml version="1.0"?>
<Systems>
<Group name="Group1">
<HostSystem name="System1" IPAddress="1.1.1.1"> </HostSystem>
<HostSystem name="System2" IPAddress="2.2.2.2"> </HostSystem>
<HostSystem name="System3" IPAddress="3.3.3.3"> </HostSystem>
</Group>
<Group name="Group2">
<HostSystem name="System4" IPAddress="4.4.4.4"> </HostSystem>
<HostSystem name="System5" IPAddress="5.5.5.5"> </HostSystem>
<HostSystem name="System6" IPAddress="6.6.6.6"> </HostSystem>
</Group>
您不能从 GUI 线程以外的线程访问控件。
一个快速解决方法是通过在你的构造函数中保存主 Dispatcher
对象并使用它来调用你的追加(使用 BeginInvoke
)来将你的 AppendText
分派到主线程主线程。
您可能应该改进的其他事情包括不实例化多个 Ping
对象并且根本不使用富文本框,您拥有的是日志条目行,它们可以更好地建模为可观察数组绑定到 ItemsControl
.
的字符串
编辑:还使用 Path.Combine
组合路径,您的字符串连接非常脆弱。
我对你的设计和代码有足够的评论,但为了帮助你解决你的问题,如何从线程更新结果框:
if (resultsBox.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate()
{
// you can do anything to resultBox here (1)
});
}
else
{
// do it here (2)
}
为了避免 1 和 2 处的重复代码,请创建一个新方法 UpdateResultBox() 来放置您的所有逻辑,您的代码将是:
if (resultsBox.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate()
{
UpdateResultsBox();
});
}
else
{
UpdateResultsBox();
}
private void UpdateResultsBox()
{
// put your logic here
}
好吧,在您发布的代码中,您从未调用方法 RunPingTest
,这可能就是为什么您的 RichTextBox
永远不会更新的原因。
但是,如果您想异步执行此操作,Ping
class
包含一个名为 SendPingAsync
的方法,其中有许多重载 returns a Task<PingReply>
并允许您只使用 async/await
。无需引入任何类型的 Thread
或 BackgroundWorker
或手动处理 Dispatcher
.
private async void BtnStart_Click(object sender, RoutedEventArgs e)
{
var pingList = new List<string>
{
"1.1.1.1",
"2.2.2.2",
"3.3.3.3",
"4.4.4.4"
};
resultsBox.Document.Blocks.Clear();
resultsBox.AppendText("Doing the ping test\r");
using (var ping = new Ping())
{
foreach (var ip in pingList)
{
resultsBox.AppendText($"Pinging {ip} . . . ");
PingReply reply = await ping.SendPingAsync(ip, 2000);
resultsBox.AppendText($"{reply.Status} \r");
}
}
}
重要的是让您的事件处理程序 async void
和 await
对 SendPingAsync
的调用。您可能还需要考虑禁用 'BtnStart' 直到该方法完成,否则用户将能够在 ping 测试仍然 运行.
时单击它
我正在编写一个 C# 脚本来在网络上进行 ping 测试。除了要写入 richtextbox 的部分,我大部分 都在工作。我需要在进行 ping 测试时写下结果。我知道它需要多线程,但过去一周我一直在努力弄清楚,但做不到。这是我的代码,减去不必要的部分。我遇到麻烦的部分是 RunPingTest 函数。我还将包括我的 IP 地址示例 xml。所有按钮和其他功能都按预期工作。
using System.Diagnostics;
using System.Windows;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System;
using System.Threading;
namespace NetTestingTool
{
public partial class NetTest : Window
{
static string dataFolder = "D:";
static readonly string configPath = dataFolder + "\ConfigFiles";
//get HostIPAddresses.xml
XElement GroupNames = XElement.Load(configPath + "\HostIPAddresses.xml");
bool testIsRunning = false;
//Start the initial GUI
public NetTest()
{
InitializeComponent();
//populate the dropdown
foreach (var groupElement in GroupNames.Elements("Group"))
{
if (groupElement.Attribute("name").Value != null)
{
dropDown.Items.Add(groupElement.Attribute("name").Value);
}
}
}
//********** Button Actions **********
//run the network test
public void BtnStart_Click(object sender, RoutedEventArgs e)
{
//clear richtextbox first
resultsBox.Document.Blocks.Clear();
//first, make sure the user has selected a group
if (dropDown.SelectedItem != null)
{
string HostSelection = (string)dropDown.SelectedItem;
//create a list of IP addresses based on group chosen
List<string> pingList = new List<string>();
foreach (var groupElement in GroupNames.Elements("Group"))
{
//resultsBox.AppendText(groupElement.Attribute("name").Value);
if (groupElement.Attribute("name").Value == HostSelection)
{
// resultsBox.AppendText(groupElement);
foreach (var hostSystem in groupElement.Elements("HostSystem"))
{
if (hostSystem.Attribute("IPAddress").Value != null)
{
pingList.Add(hostSystem.Attribute("IPAddress").Value);
}
}
}
}
/*Should this be outside BtnStart_Click?
currently cannot because it uses pingList*/
void RunPingTest(object? obj)
{
//do the ping test
resultsBox.AppendText("Doing the ping test \r"); //Just kidding, it isn't
foreach (var pingElement in pingList)
{
//this needs to be able to write to resultsBox as the ping happens (richtextbox)
Ping myPing = new Ping();
resultsBox.AppendText("Pinging " + pingElement + ". . . ");
PingReply reply = myPing.Send(pingElement, 2000); //ip and timeout
string results = reply.Status.ToString();
resultsBox.AppendText(results + "\r");
}
resultsBox.AppendText("\r\n Finished ping testing");
}
}
}
//other buttons here
}
}
xml IP 地址:
<?xml version="1.0"?>
<Systems>
<Group name="Group1">
<HostSystem name="System1" IPAddress="1.1.1.1"> </HostSystem>
<HostSystem name="System2" IPAddress="2.2.2.2"> </HostSystem>
<HostSystem name="System3" IPAddress="3.3.3.3"> </HostSystem>
</Group>
<Group name="Group2">
<HostSystem name="System4" IPAddress="4.4.4.4"> </HostSystem>
<HostSystem name="System5" IPAddress="5.5.5.5"> </HostSystem>
<HostSystem name="System6" IPAddress="6.6.6.6"> </HostSystem>
</Group>
您不能从 GUI 线程以外的线程访问控件。
一个快速解决方法是通过在你的构造函数中保存主 Dispatcher
对象并使用它来调用你的追加(使用 BeginInvoke
)来将你的 AppendText
分派到主线程主线程。
您可能应该改进的其他事情包括不实例化多个 Ping
对象并且根本不使用富文本框,您拥有的是日志条目行,它们可以更好地建模为可观察数组绑定到 ItemsControl
.
编辑:还使用 Path.Combine
组合路径,您的字符串连接非常脆弱。
我对你的设计和代码有足够的评论,但为了帮助你解决你的问题,如何从线程更新结果框:
if (resultsBox.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate()
{
// you can do anything to resultBox here (1)
});
}
else
{
// do it here (2)
}
为了避免 1 和 2 处的重复代码,请创建一个新方法 UpdateResultBox() 来放置您的所有逻辑,您的代码将是:
if (resultsBox.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate()
{
UpdateResultsBox();
});
}
else
{
UpdateResultsBox();
}
private void UpdateResultsBox()
{
// put your logic here
}
好吧,在您发布的代码中,您从未调用方法 RunPingTest
,这可能就是为什么您的 RichTextBox
永远不会更新的原因。
但是,如果您想异步执行此操作,Ping
class
包含一个名为 SendPingAsync
的方法,其中有许多重载 returns a Task<PingReply>
并允许您只使用 async/await
。无需引入任何类型的 Thread
或 BackgroundWorker
或手动处理 Dispatcher
.
private async void BtnStart_Click(object sender, RoutedEventArgs e)
{
var pingList = new List<string>
{
"1.1.1.1",
"2.2.2.2",
"3.3.3.3",
"4.4.4.4"
};
resultsBox.Document.Blocks.Clear();
resultsBox.AppendText("Doing the ping test\r");
using (var ping = new Ping())
{
foreach (var ip in pingList)
{
resultsBox.AppendText($"Pinging {ip} . . . ");
PingReply reply = await ping.SendPingAsync(ip, 2000);
resultsBox.AppendText($"{reply.Status} \r");
}
}
}
重要的是让您的事件处理程序 async void
和 await
对 SendPingAsync
的调用。您可能还需要考虑禁用 'BtnStart' 直到该方法完成,否则用户将能够在 ping 测试仍然 运行.