Time limit when entering choice

From TRCCompSci - AQA Computer Science
Revision as of 13:03, 24 May 2018 by Jared (talk | contribs) (Created page with "== Extra declarations and namespacing == <syntaxhighlight lang="C#"> using System.Timers;//Add the Timer class to allow checking how much real time has passed...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Extra declarations and namespacing

using System.Timers;//Add the Timer class to allow checking how much real time has passed
        

        private static Timer T = new Timer(5000);//Create a new instance of the class Timer with a 5 second delay between event calls
        private static bool Timeout = false;//A variable to check whether time has run out

GetChoice

private static string GetChoice()
        {
            Timeout = false; //This ensures that each time a player enters a choice they have the right amount of time.
            
            // . . .
            
            T.Start();//This starts counting real time using an event.
            T.Elapsed += T_Elapsed;//This subscribes the event to a delegate, it allows a method to run after a set amount of time
            
            // . . .
            
            //The following if clears the choice to make it tidier
            if (Timeout)
            {
                Choice = "";
            }

            // . . .
        }