Use the await
keyword:
hashSet = await Client.getLinks("http://www." + textBox1.Text);
Question
I have async method:
public static async Task<HashSet<string>> getLinks(string page)
{
// GET request
string str = await Client.get(page);
// Pattern for http address
Regex regx = new Regex(@"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)", RegexOptions.IgnoreCase);
str = str.Replace(" ", " ");
// Get collection of strings
MatchCollection matches = regx.Matches(str);
// Add to the hash set
HashSet<string> hashSet = new HashSet<string>();
foreach (Match match in matches)
{
hashSet.Add(match.Value);
}
// Return set of url
return hashSet;
}
How can I call it without creating other methods? I tried this way:
HashSet<string> hashSet = new HashSet<string>();
hashSet = Client.getLinks("http://www." + textBox1.Text);
But got an error: Can not convert Task<HashSet<string>> to HashSet<string>.
Then I tried:
hashSet = Client.getLinks("http://www." + textBox1.Text).Result;
But it doesn't work.
La solution
Use the await
keyword:
hashSet = await Client.getLinks("http://www." + textBox1.Text);