All this await stuff in C# All this await stuff in C#
Posts 1 to 7 of 7
  1. #1

    My post count is Majestic

    d000hg's Avatar
    Join Date
    Dec 2007
    Location
    My house
    Posts
    32,739

    Default All this await stuff in C#

    I've not MT code in C# for years and when I did it was all the old-fashioned way... basically signals and threads and stuff like in C++.
    The asynchronous revolution broadly passed me by and now I'm writing a MT application... it feels a great opportunity to bone up on the modern way but online resources are muddled depending when they were written.

    Can anyone recommend a book on modern C# for someone who has worked with it but is out of date with this stuff?
    Quote Originally Posted by MaryPoppins View Post
    I'd still not breastfeed a nazi
    Quote Originally Posted by vetran View Post
    Urine is quite nourishing

  2. #2

    Richer than sasguru

    DimPrawn's Avatar
    Join Date
    Jul 2005
    Location
    Covid City
    Posts
    35,822
    SARS-CoV-2 - Finally something from China that still works as designed.

  3. #3

    My post count is Majestic

    d000hg's Avatar
    Join Date
    Dec 2007
    Location
    My house
    Posts
    32,739

    Default

    Quote Originally Posted by DimPrawn View Post
    I never used these sorts of thing, would you endorse them as an experienced developer?
    Quote Originally Posted by MaryPoppins View Post
    I'd still not breastfeed a nazi
    Quote Originally Posted by vetran View Post
    Urine is quite nourishing

  4. #4

    Super poster

    Hobosapien's Avatar
    Join Date
    Feb 2016
    Location
    LA - la la fantasy land
    Posts
    2,954

    Default

    Books are old school but as everyone has their own level of experience, goals, and desire for learning just google 'c# books' and see what is currently recommended for the level you feel you're at.

    Alternatively start at the beginning as a refresher by binge watching the following:

    c# tutorial for beginners

    102 videos. Shows how the language is broken down so you can seek out more advanced videos or courses for any aspects of particular interest. The 'await/async' stuff is covered in video 101.
    Maybe tomorrow, I'll want to settle down. Until tomorrow, I'll just keep moving on.

  5. #5

    Contractor Among Contractors

    Dark Black's Avatar
    Join Date
    Apr 2007
    Location
    the post-apocalyptic wasteland
    Posts
    1,203

    Default

    Unless you are doing web page handling, my advice is to not use the await stuff and just do your multithreading the old school way, create and manage your own threads.

    Others will disagree of course.
    Do what thou wilt

  6. #6

    Fingers like lightning


    Join Date
    Feb 2010
    Posts
    614

    Default

    using System;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static async Task Main(string[] args)
    {
    Console.WriteLine("Coronavirus Supermarket visit");
    Coronavirus coronavirus = new Coronavirus();
    Console.WriteLine("Starting looking for essentials ...");
    Task<bool> handSanitiserTask = coronavirus.FindHandSanitiser();
    Task<bool> toiletRollTask = coronavirus.FindToiletRoll();
    await handSanitiserTask;
    Console.WriteLine("Found sanitiser !!!!! ");
    await toiletRollTask;
    Console.WriteLine("Found toilet roll !!!!! ");
    }
    }

    class Coronavirus
    {
    public Coronavirus()
    {
    }

    public async Task<bool> FindHandSanitiser()
    {
    for (int i = 0; i < 10; i++)
    {
    Console.WriteLine("Looking for hand sanitiser");
    await Task.Delay(1000);
    }
    return true;
    }

    public async Task<bool> FindToiletRoll()
    {
    for (int i = 0; i < 10; i++)
    {
    Console.WriteLine("Looking for toilet roll");
    await Task.Delay(2000);
    }
    return true;
    }
    }
    }

    /*
    Example output from a run

    Coronavirus Supermarket visit
    Starting looking for essentials ...
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Found sanitiser !!!!!
    Looking for toilet roll
    Looking for toilet roll
    Looking for toilet roll
    Looking for toilet roll
    Found toilet roll !!!!!
    */


  7. #7

    My post count is Majestic

    NickFitz's Avatar
    Join Date
    Jun 2007
    Location
    Your local branch
    Posts
    49,393

    Default

    Quote Originally Posted by SunnyInHades View Post
    <snip>
    If you use the [code]…[/code] tags (the # icon in the editor toolbar) it’ll use a monospace font and retain the whitespace:

    Code:
    using System;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                Console.WriteLine("Coronavirus Supermarket visit");
                Coronavirus coronavirus = new Coronavirus();
                Console.WriteLine("Starting looking for essentials ...");
                Task<bool> handSanitiserTask = coronavirus.FindHandSanitiser();
                Task<bool> toiletRollTask = coronavirus.FindToiletRoll();
                await handSanitiserTask;
                Console.WriteLine("Found sanitiser !!!!! ");
                await toiletRollTask;
                Console.WriteLine("Found toilet roll !!!!! ");
            }
        }
    
        class Coronavirus
        {
            public Coronavirus()
            {
            }
    
            public async Task<bool> FindHandSanitiser()
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Looking for hand sanitiser");
                    await Task.Delay(1000);
                }
                return true;
            }
    
            public async Task<bool> FindToiletRoll()
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("Looking for toilet roll");
                    await Task.Delay(2000);
                }
                return true;
            }
        }
    }
    
    /* 
    Example output from a run
    
    Coronavirus Supermarket visit
    Starting looking for essentials ...
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Looking for hand sanitiser
    Looking for hand sanitiser
    Looking for toilet roll
    Found sanitiser !!!!!
    Looking for toilet roll
    Looking for toilet roll
    Looking for toilet roll
    Looking for toilet roll
    Found toilet roll !!!!!
    */
    OK, you also have to scroll a bit to read it, but nothing’s perfect

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •