我正在编写一个应用程序,我必须扫描一个ip(内联网)范围,看看特定的ip是否与特定网址相对应。例如,假设我们有url:http://<ip>:8080/x/y
,我们想知道是否可以找到在192.168.1.1 - 192.168.1.254范围内运行的活动服务器。显然,扫描过程不应该阻止UI。
所以我创建了以下异步方法:
private List<AvailableServer> _availableServerList;
public List<AvailableServer> AvailableServerList
{
get { return _availableServerList;}
set { _availableServerList = value;}
}
private Mutex objLock = new Mutex(true, "MutexForScanningServers");
private long IPsChecked;
private List<string> allPossibleIPs;
CancellationTokenSource cts;
public async Task GetAvailableServers()
{
Dictionary<string, string> localIPs = LocalIPAddress(); //new Dictionary<string, string>();
allPossibleIPs = new List<string>();
Dictionary<string, CancellationToken> kvrList = new Dictionary<string, CancellationToken>();
cts = new CancellationTokenSource();
foreach (KeyValuePair<string, string> kvp in localIPs)
{
allPossibleIPs.AddRange(getIpRange(kvp.Key.ToString(), kvp.Value.ToString()));
}
foreach (string ip in allPossibleIPs)
{
kvrList.Add(ip, cts.Token);
}
AvailableServerList = new List<AvailableServer>();
var downloads = kvrList.Select(kvr => isServerAvailableAsync(kvr));
Task[] dTasks = downloads.ToArray();
await Task.WhenAll(dTasks).ConfigureAwait(false);
}
其目的是启动一系列任务,每个人都试图接收有效的HttpClient请求。假设HttpClient.GetStringAsync没有抛出任何异常,请求是有效的:
private async Task isServerAvailableAsync(Object obj)
{
using (var client = new HttpClient())
{
try
{
KeyValuePair<string, CancellationToken> kvrObj = (KeyValuePair<string, CancellationToken>)obj;
string urlToCheck = @"http://" + kvrObj.Key + ":8080/x/y";
string downloadTask = await client.GetStringAsync(urlToCheck).ConfigureAwait(false);
string serverHostName = Dns.GetHostEntry(kvrObj.Key).HostName;
AvailableServerList.Add(new AvailableServer(serverHostName, @"http://" + serverHostName + ":8080/x/y"));
// }
}
catch (System.Exception ex)
{
//...Do nothing for now...
}
finally
{
lock (objLock)
{
//kvrObj.Value.ThrowIfCancellationRequested();
IPsChecked++;
int tmpPercentage = (int)((IPsChecked * 100) / allPossibleIPs.Count);
if (IPsCheckedPercentageCompleted < tmpPercentage)
{
IPsCheckedPercentageCompleted = tmpPercentage;
OnScanAvailableServersStatusChanged(new EventArgs());
}
}
}
}
}
如果请求有效,那么我们找到了一个可用的服务器,因此我们将url添加到我们的列表中。否则我们会遇到异常。最后,我们更新了百分比变量,因为我们希望将它们传递给我们的UI(扫描xx%)。每次任务完成时,它都会触发一个委托,我们的UI使用该委托来获取新的可用服务器更新列表并完成百分比。 主异步函数GetAvailableServers通过Task.Run(()=&gt; className.GetAvailableServers())开始运行,该任务存在于DelegateCommand中,该委托命令驻留在我自己的ViewModel中:
public ICommand GetAvailableServersFromViewModel
{
get
{
return new DelegateCommand
{
CanExecuteFunc = () => true,
CommandAction = () =>
{
Task.Run(() => utilsIp.GetAvailableServers());
}
};
}
}
我的实现问题是UI在扫描时滞后,这很容易通过我在UI中的加载微调器看到。那么代码远非最好,我知道我错了。