Aborting a timer/command

Aborting a timer/command

Postby Clowny » 31 Jul 2013, 03:34

alright so here is part of a code for a timer
Code: Select all
   {
            int seconds;

            if (!int.TryParse(message, out seconds) || int.Parse(message) <= 0 || int.Parse(message) > 1000)
            {
                Player.SendMessage(p, "Invalid amount of seconds.");
                return;
            }

            for (int i = seconds; i >= 0; i--)
            {
                Player.SendMessage(p, "Seconds remaining: " + i.ToString());
                Thread.Sleep(1000);
            }
        }

Now I know it has something to do with for loop but I don't quite understand how to put it in could somebody do it and explain how it works
Founder Of MC Classic Hosting Community http://mcclassichosting.webs.com
Founder of MC Classic Software MCReborn http://mcreborn.tk
Professional Hoster and Basic C# Coder
Image
User avatar
Clowny
 
Posts: 112
Joined: 14 Jul 2013, 03:53

Re: Aborting a timer/command

Postby ismellike » 31 Jul 2013, 04:01

Put "bool enabled = false;" outside of the use method.
Then inside of the for loop, put "if(enabled)" do timer stuff.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Aborting a timer/command

Postby Conor » 31 Jul 2013, 04:56

You should learn how to use a System.Timers.Timer. It will help you out in the future, it is designed to be used as a timer (quite obviously) whereas there may be some vulnerabilities if you rely on Thread.Sleep method all of the time. You have many more options and a greater efficiency using a Timer too.

Here is an example for you.

Code: Select all
        // The millisecond interval between each time the timer ticks
        public static int tick = 1000;

        // The timer
        public System.Timers.Timer myTimer = new System.Timers.Timer(tick);

        // The time you want the timer to last for
        public static int timerSeconds = 60;

        // Just some method you will be using in your project as an example
        public void DoStuff(string message)
        {
            if (message == "stop" || message == "abort")
            {
                myTimer.Stop();
            }
            else if (message == "start")
            {
                StartTimer();
            }
        }

        // Assigns the tick event to the myTimer_Elapsed method
        public void StartTimer()
        {
            myTimer.Elapsed += myTimer_Elapsed;
            myTimer.Start();
        }

        // Called when the timer ticks (event)
        private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("Timer has ticked.");
            timerSeconds--;

            // If the duration has finished, stop the timer.
            if (timerSeconds == 0)
            {
                myTimer.Stop();
            }
        }
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Aborting a timer/command

Postby Clowny » 31 Jul 2013, 06:06

ismellike wrote:Put "bool enabled = false;" outside of the use method.
Then inside of the for loop, put "if(enabled)" do timer stuff.

Could you show me that on the actual code hehe
Founder Of MC Classic Hosting Community http://mcclassichosting.webs.com
Founder of MC Classic Software MCReborn http://mcreborn.tk
Professional Hoster and Basic C# Coder
Image
User avatar
Clowny
 
Posts: 112
Joined: 14 Jul 2013, 03:53

Re: Aborting a timer/command

Postby Warren1001 » 31 Jul 2013, 06:44

Clowny wrote:
ismellike wrote:Put "bool enabled = false;" outside of the use method.
Then inside of the for loop, put "if(enabled)" do timer stuff.

Could you show me that on the actual code hehe

Code: Select all
using System;
namespace MCDzienny
{
   public class Cmd : Command
   {
      bool enabled = false;
      public override string name { get { return ""; } }
      public override string shortcut { get { return ""; } }
      public override string type { get { return ""; } }
      public override bool museumUsable { get { return ; } }
      public override LevelPermission defaultRank { get { return LevelPermission.; } }
      public override Use(Player p, string message)
      {
         int seconds;

         if (!int.TryParse(message, out seconds) || int.Parse(message) <= 0 || int.Parse(message) > 1000)
         {
            Player.SendMessage(p, "Invalid amount of seconds.");
            return;
         }

         if (enabled == false)
         {
            enabled = true;
            for (int i = seconds; i >= 0; i--)
            {
               Player.SendMessage(p, "Seconds remaining: " + i.ToString());
               Thread.Sleep(1000);
            }
         }

         enabled = false;
      }
      public override Help(Player p)
      {
      }
   }
}
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: Aborting a timer/command

Postby Clowny » 31 Jul 2013, 18:01

Um ok but how do I put it into play such as doing /advtimer abort and then it stops..?
Founder Of MC Classic Hosting Community http://mcclassichosting.webs.com
Founder of MC Classic Software MCReborn http://mcreborn.tk
Professional Hoster and Basic C# Coder
Image
User avatar
Clowny
 
Posts: 112
Joined: 14 Jul 2013, 03:53


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 3 guests

cron