Home

C# Most Very Basic Async

The line to note here is... "Task.Run(() => x.GeturlsAsync());". What we're saying is "run this function asynchronously". That means when GeturlsAsync is doing it's thing, the rest of the code can work too. This need to be run in a console app, .Net Core 6

using System.Diagnostics;
using System.Net;

MyProgram x = new MyProgram();
x.Fillurls(x);
Task.Run(() => x.GeturlsAsync());
x.v1();
x.v2();
x.v3();
x.v4();

Console.ReadLine();


public class MyProgram
{
    public List<string> urls = new List<string>();
  
    public void Fillurls(MyProgram x)
    {
        urls.Add("https://www.google.com");
        urls.Add("https://bbc.co.uk");
        urls.Add("https://msn.com");
        urls.Add("https://facebook.com");
        urls.Add("https://www.cnn.com");
    }

    public void GeturlsAsync()
    {
        WebClient client = new WebClient();

        Stopwatch watch = new Stopwatch();
        watch.Start();

        foreach (string x in urls)
        {
            Console.WriteLine(x);
            string y = client.DownloadString(x);
            Console.WriteLine(y.Substring(0, 100));
        }

        watch.Stop();
        Console.WriteLine("TIME------------- " + watch.Elapsed.ToString());
    }

    public void v1()
    {
        Console.WriteLine("Void 1");
    }

    public void v2()
    {
        Console.WriteLine("Void 2");
    }

    public void v3()
    {
        Console.WriteLine("Void 3");
    }

    public void v4()
    {
        Console.WriteLine("Void 4");
    }
}

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog