如何使用异步方法查找Internet连接 [英] How to find internet connection using async method
本文介绍了如何使用异步方法查找Internet连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void isConnected()
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://www.google.com"));
}
static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
e.Result();
}
当我在下面找到解决方案时出现错误消息
`不可调用的成员'System.Net.OpenReadCompletedEventArgs.Result'不能像方法一样使用`
请帮助我
when bulid the solution i got below error message
`Non-invocable member 'System.Net.OpenReadCompletedEventArgs.Result' cannot be used like a method`
pls help me
推荐答案
结果
是属性,而不是方法。请参阅 https://msdn.microsoft.com/en- us / library / system.net.openreadcompletedeventargs_properties(v = vs.110).aspx [ ^ ]。
Result
is a property, not a method. See https://msdn.microsoft.com/en-us/library/system.net.openreadcompletedeventargs_properties(v=vs.110).aspx[^].
将代码更改为以下内容。
Change your code to below.
public static void isConnected()
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("http://www.google.com"));
}
static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
reply = (Stream)e.Result;
s = new StreamReader (reply);
string strResult = s.ReadToEnd ();
}
这篇关于如何使用异步方法查找Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文