【发布时间】:2013-12-11 19:56:54
【问题描述】:
我正在尝试创建一个多线程应用程序,该应用程序允许我 ping 数千台主机,ping 的结果将写入富文本框。
此应用程序执行后,一旦它遍历了一千个左右的地址,就会出现以下异常:
检测到DisconnectedContext 消息:此 RuntimeCallableWrapper 转换到 COM 上下文 0x4410e0 失败,出现以下错误:系统调用失败。 (来自 HRESULT 的异常:0x80010100 (RPC_E_SYS_CALL_FAILED))。这通常是因为创建此 RuntimeCallableWrapper 的 COM 上下文 0x4410e0 已断开连接,或者它正忙于做其他事情。从当前 COM 上下文(COM 上下文 0x440f70)释放接口。这可能会导致损坏或数据丢失。为避免此问题,请确保所有 COM 上下文/单元/线程保持活动状态并且可用于上下文转换,直到应用程序完全使用 RuntimeCallableWrapper 完成,该 RuntimeCallableWrapper 表示存在于其中的 COM 组件。
我不完全确定是什么原因造成的,起初我认为这是由于我没有处理 Ping,但我已经解决了这个问题,问题仍然存在。
如果有人有这方面的任何信息,将不胜感激。
谢谢大家。
public static void LogTextEvent(RichTextBox TextEventLog, Color TextColor, string EventText)
{
if (TextEventLog.InvokeRequired)
{
TextEventLog.BeginInvoke(new Action(delegate { LogTextEvent(TextEventLog, TextColor, EventText); }));
return;
}
string nDateTime = DateTime.Now.ToString("hh:mm:ss tt") + " - ";
// color text.
TextEventLog.SelectionStart = TextEventLog.Text.Length;
TextEventLog.SelectionColor = TextColor;
// newline if first line, append if else.
if (TextEventLog.Lines.Length == 0)
{
TextEventLog.AppendText(nDateTime + EventText);
TextEventLog.ScrollToCaret();
TextEventLog.AppendText(Environment.NewLine);
}
else
{
TextEventLog.AppendText(nDateTime + EventText + Environment.NewLine);
TextEventLog.ScrollToCaret();
}
}
private void button1_Click(object sender, EventArgs e)
{
string[] logFile = File.ReadAllLines("addrs.txt");
var addresses = new List<string>(logFile);
foreach (string ip in addresses)
{
// See http://stackoverflow.com/questions/4744630/unexpected-behaviour-for-threadpool-queueuserworkitem
// for reason to use another variable in the loop
string loopIp = ip;
WaitCallback func = delegate
{
if (PingIP(loopIp))
{
LogTextEvent(richTextBox1, Color.Green, "[ " + loopIp.ToUpper() + " ] - Ping Success");
}
else
{
LogTextEvent(richTextBox1, Color.Red, "[ " + loopIp.ToUpper() + " ] - Ping FAIL!");
}
};
ThreadPool.QueueUserWorkItem(func);
}
}
public static bool PingIP(string IP)
{
bool result = false;
var ping = new Ping();
try
{
//var ping = new Ping();
PingReply pingReply = ping.Send(IP);
if (pingReply.Status == IPStatus.Success)
result = true;
}
catch
{
result = false;
}
finally
{
ping.Dispose();
}
return result;
}
【问题讨论】:
-
尝试使用一些合理的值调用 SetMaxThreads - 尝试 50。
-
很遗憾没用,不过还是感谢您的建议。
标签: c# multithreading threadpool