简体   繁体   中英

Should I Be Using Async Calls?

I have ac# application which reads a table of roughly 1500 site url's of clients who have been with the company since we started. Basically, I am running whois queries on these url's and seeing if they are still a client or not. The application works but it takes roughly an hour to complete. Would I be better off using async whois queries and how much time roughly could I save. Here is a sample whois query block of code that I am using.

Also if anyone has any tips on how to improve this code or run async commands could ye please help me out as I'm only an intern. Thanks

string whoisServer = "whois.markmonitor.com";
    string data;
    try
    {
        TcpClient objTCPC = new TcpClient(whoisServer, 43);
        string strDomain = domainName + "\r\n";
        byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain);

        Stream objStream = objTCPC.GetStream();
        objStream.Write(arrDomain, 0, strDomain.Length);
        StreamReader objSR = new StreamReader(objTCPC.GetStream(),
        Encoding.ASCII);
        //return objSR.ReadLine();
        //return (Regex.Replace(objSR.ReadToEnd(),"\n","<br>")).ToString();
        using (StreamReader reader = new StreamReader(objTCPC.GetStream(), Encoding.ASCII))
        {
            data = (reader.ReadToEnd());
        }
        //test.Add(objSR.ReadLine());
        objTCPC.Close();
    }
    catch
    {
        data = "Not Found";
    }
    return data;

Well, the short answer is certainly yes.

Since you are making multiple, completely independent lookups, you have everything to gain by running them in parallel, asynchronously.

There are several ways to do this. The options depend on what version of .net you're in.

As you would guess, there are many examples.
Check these out right here on SO.

Avaliable parallel technologies in .Net
Multi threaded file processing with .NET
When to use a Parallel.ForEach loop instead of a regular foreach?

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