阅读有关C#BackgroundWorker的大多数(所有?)已回答的问题,但似乎没有一个适用于这种情况。如果我错过了一个,请指出我的方向!
无论如何,我遇到麻烦让Ping进程作为后台进程运行。我做了一个简单的表单应用程序发送ping和报告。这很好但但只有在ping完成后才会给用户带来结果 - 因此需要后台进程。我对C#不太熟悉,并且不熟悉BackgroundWorker的细节。但是,在这里找到了Microsoft的有用演练:http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx
我现在正尝试将相同的进程应用于System.Net.NetworkInformation对象而不是System.IO.StreamReader对象。我认为我非常接近(阅读:我可以让应用程序构建和运行)但我在运行时始终遇到错误(见下文)。
这是他们的示例应用程序的Microsoft代码。它就像一个冠军:
MainForm.cs中调用演练
中引用的Words.cs类的方法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中最后一个方法的最后一行(var名称不同,但你明白了):
void Bw01DoWork(object sender, DoWorkEventArgs e)
{
System.ComponentModel.BackgroundWorker worker;
worker = (System.ComponentModel.BackgroundWorker)sender;
PTResults PR = (PTResults)e.Argument;
PR.SendPings(worker, e); // Line 152
}
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进行编码。
答案 0 :(得分:0)
看一下Ping SendAsync,您可以消除大部分代码 - 只需调用PingAsync,并处理结果,确保将其分派到UI线程,然后重新排队另一个调用。
http://msdn.microsoft.com/en-us/library/ms144961(v=vs.110).aspx