none
Help with code RRS feed

  • Question

  • Hello,

    I am new to Visual Studios C# and I am trying to write a code that describes a process of a machine. I want to add a 4-5 second delay after one of the WriteLine messages and a count down timer. I was wondering how I would do that? (I am using .NET framework in Visual Studios C#)

    Here is a rough sample of my code:

    Console.WriteLine("Motor initiating...Please wait...");

    //Add 4-5 second delay before next message pops up 

    Console.WriteLine("Motor On. Please wait 60 seconds...");

    //Add countdown timer from 60 seconds. Once timer ends, display next message.

    Console.Writeline("Motor off. Opening Release valve...");


    Thanks in advance.

    Friday, February 28, 2020 5:40 PM

Answers

  • The following can be done in any type of project, console apps are easy to show.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp2Async
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                Console.WriteLine("Motor initiating...Please wait...");
                await Task.Delay(5000);
                //Add 4-5 second delay before next message pops up 
    
                Console.WriteLine("Motor On. Please wait 60 seconds...");
                await Task.Delay(6000);
                //Add countdown timer from 60 seconds. Once timer ends, display next message.
    
                Console.WriteLine("Motor off. Opening Release valve...");
    
                await Task.Delay(1000);
    
                Console.WriteLine("Done");
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Extra Account Friday, February 28, 2020 6:22 PM
    Friday, February 28, 2020 5:55 PM
    Moderator

All replies

  • Friday, February 28, 2020 5:46 PM
  • The following can be done in any type of project, console apps are easy to show.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp2Async
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                Console.WriteLine("Motor initiating...Please wait...");
                await Task.Delay(5000);
                //Add 4-5 second delay before next message pops up 
    
                Console.WriteLine("Motor On. Please wait 60 seconds...");
                await Task.Delay(6000);
                //Add countdown timer from 60 seconds. Once timer ends, display next message.
    
                Console.WriteLine("Motor off. Opening Release valve...");
    
                await Task.Delay(1000);
    
                Console.WriteLine("Done");
                Console.ReadLine();
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Extra Account Friday, February 28, 2020 6:22 PM
    Friday, February 28, 2020 5:55 PM
    Moderator
  • Thanks. That helped a lot!
    Friday, February 28, 2020 6:22 PM
  • It looks like you already have an answer to your immediate questions, but in case you need to add more features to your simulation, let me suggest a different architecture.

    Consider having a queue of event objects, where each event object has a "start time" and a "description".  Then, your main program just becomes a big loop, in which it delays 1 second, bumps a counter, and then pops and prints any events that match the current simulated time.

    Now you can make your simulation a bit more realistic.  You can queue up a "motor initiating" event, and when that event fires, it can add a new event for 4 seconds in the future for "on".  The "motor on" event can then add a "motor off" event for 60 seconds in the future.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, March 1, 2020 8:19 AM