У мене є метод асинхронізації:
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;
}
Як я можу це назвати, не створюючи інших методів? Я спробував так:
HashSet<string> hashSet = new HashSet<string>();
hashSet = Client.getLinks("http://www." + textBox1.Text);
Але сталася помилка: Can not convert Task<HashSet<string>> to HashSet<string>.
Тоді я спробував:
hashSet = Client.getLinks("http://www." + textBox1.Text).Result;
Але це не працює.
Відповіді:
3 для відповіді № 1Використовувати await
ключове слово:
hashSet = await Client.getLinks("http://www." + textBox1.Text);