简体   繁体   中英

C#. Calling async method

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.

使用await关键字:

hashSet = await Client.getLinks("http://www." + textBox1.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM