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\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)", RegexOptions.IgnoreCase);
        str = str.Replace("&nbsp;", " ");

        // 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.

有帮助吗?

解决方案

Use the await keyword:

hashSet = await Client.getLinks("http://www." + textBox1.Text);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top