让C#BackgroundWorker进程调用ping
通读关于C#BackgroundWorker的大部分(全部?)回答问题,但似乎没有一个适用于这种情况。如果我错过了一个,请告诉我那个方向 无论如何,我很难让Ping进程作为后台进程运行。我做了一个简单的表单应用程序来发送ping并返回报告。这很好,但它只会在ping完成后向用户显示结果——因此需要一个后台进程。我对C#有点陌生,对幕后工作人员的细节不太熟悉。但是,您在此处找到了Microsoft提供的有用演练: 我现在尝试将相同的过程应用于System.Net.NetworkInformation对象,而不是System.IO.StreamReader对象。我认为我真的很接近(阅读:我可以让应用程序构建和运行),但我总是在运行时出错(见下文) 这是他们示例应用程序的Microsoft代码。它就像一个冠军: MainForm.cs中调用演练中引用的Words.cs类的方法让C#BackgroundWorker进程调用ping,c#,.net,winforms,backgroundworker,sharpdevelop,C#,.net,Winforms,Backgroundworker,Sharpdevelop,通读关于C#BackgroundWorker的大部分(全部?)回答问题,但似乎没有一个适用于这种情况。如果我错过了一个,请告诉我那个方向 无论如何,我很难让Ping进程作为后台进程运行。我做了一个简单的表单应用程序来发送ping并返回报告。这很好,但它只会在ping完成后向用户显示结果——因此需要一个后台进程。我对C#有点陌生,对幕后工作人员的细节不太熟悉。但是,您在此处找到了Microsoft提供的有用演练: 我现在尝试将相同的过程应用于System.Net.NetworkInformatio
void backgroundWorker1DoWork(object sender, DoWorkEventArgs e)
{
System.ComponentModel.BackgroundWorker worker;
worker = (System.ComponentModel.BackgroundWorker)sender;
Words WC = (Words)e.Argument;
WC.CountWords(worker, e);
}
“Words.cs”课程中的相关方法
public void CountWords(
System.ComponentModel.BackgroundWorker worker,
System.ComponentModel.DoWorkEventArgs e)
{
// Initialize the variables.
CurrentState state = new CurrentState();
string line = "";
int elapsedTime = 20;
DateTime lastReportDateTime = DateTime.Now;
if (CompareString == null ||
CompareString == System.String.Empty)
{
throw new Exception("CompareString not specified.");
}
// Open a new stream.
using (System.IO.StreamReader myStream = new System.IO.StreamReader(SourceFile))
{
// Process lines while there are lines remaining in the file.
while (!myStream.EndOfStream)
{
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
line = myStream.ReadLine();
WordCount += CountInString(line, CompareString);
LinesCounted += 1;
// Raise an event so the form can monitor progress.
int compare = DateTime.Compare(
DateTime.Now, lastReportDateTime.AddMilliseconds(elapsedTime));
if (compare > 0)
{
state.LinesCounted = LinesCounted;
state.WordsMatched = WordCount;
worker.ReportProgress(0, state);
lastReportDateTime = DateTime.Now;
}
}
// Uncomment for testing.
System.Threading.Thread.Sleep(5);
}
// Report the final count values.
state.LinesCounted = LinesCounted;
state.WordsMatched = WordCount;
worker.ReportProgress(0, state);
}
}
当我尝试类似的过程(发送Ping而不是读取文件)时,会出现以下错误:
Error: Object reference not set to an instance of an object.
Details: System.Collections.ListDictionaryInternal //This is defined in the MyApp namespace as: using System.Collections
Source: MyApp
StackTrack: at MyApp.MainForm.Bw01DoWork(Object sender, DoWorkEventArgs e) in
[path]\MainForm.cs:line 152
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
Target: Void Bw01DoWork(System.Object, System.ComponentModel.DoWorkEventArgs)
这是我的方法。错误中引用的第152行是MainForm.cs中最后一个方法的最后一行(变量名称不同,但您知道了):
以及PTResults.cs类的相关部分:
using (Ping newPing = new Ping())
{
PingReply reply = newPing.Send([Target Site],[Timeout]);
if(reply.Status == IPStatus.Success)
{
state.PingOK = true;
}
else if(reply.Status == IPStatus.TimedOut)
{
state.PingOK = false;
state.PingUpdateState = " Timed Out";
}
else if(reply.Status != IPStatus.Success)
{
state.PingOK = false;
state.PingUpdateState = " FAILED";
}
else
{
state.PingOK = false;
state.PingUpdateState = " UNKNOWN";
}
worker.ReportProgress(0, state.PingOK);
}
我认为System.Net.NetworkInformation.Ping组件不能像System.IO.StreamReader那样调用。想法
我怀疑这会有什么不同,但FWIW我正在Windows 8.1系统上使用SharpDevelop进行编码。看看Ping SendAsync,您可能能够消除大部分代码-只需调用PingAsync,并处理结果,确保将其分派到UI线程,然后重新排队等待另一个调用
我的回答有帮助吗?还没有。我正在修补PingAsync。然而,我仍然不知道为什么我的代码写得不好(因为IO流工作得很好)。甚至可以用这种方式使用Ping吗,或者PingAsync是这里唯一的实际选项吗?
using (Ping newPing = new Ping())
{
PingReply reply = newPing.Send([Target Site],[Timeout]);
if(reply.Status == IPStatus.Success)
{
state.PingOK = true;
}
else if(reply.Status == IPStatus.TimedOut)
{
state.PingOK = false;
state.PingUpdateState = " Timed Out";
}
else if(reply.Status != IPStatus.Success)
{
state.PingOK = false;
state.PingUpdateState = " FAILED";
}
else
{
state.PingOK = false;
state.PingUpdateState = " UNKNOWN";
}
worker.ReportProgress(0, state.PingOK);
}