【发布时间】:2013-12-15 11:13:57
【问题描述】:
我有一个后台工作人员。它工作得很好但是我想把它用完的代码移出 DoWork 这可能吗?
void NewWorker_DoWork(object sender, DoWorkEventArgs e)
{
List<string> ReturnResults = new List<string>();
BackgroundWorker worker = sender as BackgroundWorker;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select StatusCode from Win32_PingStatus where address = 'Metabox-PC'");
ManagementObjectCollection objCollection = searcher.Get();
foreach (ManagementObject Results in objCollection)
{
ReturnResults.Add(Results["StatusCode"].ToString());
}
e.Result = ReturnResults;
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(1);
}
所以它实际上是在哪里查询 WMI,我希望能够在其中添加我喜欢的任何计算机。
这可能吗?
public void StartBackgroundWorker()
{
BackgroundWorker NewWorker = new BackgroundWorker();
NewWorker.DoWork += new DoWorkEventHandler(NewWorker_DoWork);
NewWorker.ProgressChanged += new ProgressChangedEventHandler(NewWorker_ProgressChanged);
NewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(NewWorker_RunWorkerCompleted);
NewWorker.WorkerReportsProgress = true;
NewWorker.RunWorkerAsync();
}
void NewWorker_DoWork(object sender, DoWorkEventArgs e)
{
List<string> ReturnResults = new List<string>();
BackgroundWorker worker = sender as BackgroundWorker;
BackgroundWorkerOperations NewOperation = new BackgroundWorkerOperations();
NewOperation.Operations(GlobalComputerName);
e.Result = ReturnResults;
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(1);
}
public class BackgroundWorkerOperations
{
public Operations(string ComputerNames)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select StatusCode from Win32_PingStatus where address = '" + ComputerNames + '");
ManagementObjectCollection objCollection = searcher.Get();
foreach (ManagementObject Results in objCollection)
{
ReturnResults.Add(Results["StatusCode"].ToString());
}
e.Result = ReturnResults;
}
}
【问题讨论】:
标签: wpf backgroundworker