none
C# stop/start code from - to day of week and time RRS feed

  • Question

  • Hello

    I have 2 methods, Stop(); and Start(); 

    what i need is to call Stop(); from Friday 23:00:00 until Sunday 22:59:00 and call Start(); again in Sunday 23:00:00

    I tried this but doesn't work, it run code Start(); in between

    DateTime time = DateTime.Now;
    var date = DateTime.Now.ToString("HH:mm:ss");
    
    if (date == "23:00:00" && time.DayOfWeek == DayOfWeek.Friday && 
        date == "23:00:00" && time.DayOfWeek == DayOfWeek.Saturday &&
        date == "22:59:00" && time.DayOfWeek == DayOfWeek.Sunday)
    {
        Stop();
    } else {
       Start();
    }


    Friday, January 11, 2019 11:17 AM

Answers

  • Hello,

    Perhaps base the decision on HH:MM.

    public class Operations
    {
        public bool Decide()
        {
            var result = false;
            if ((DateTime.Now.DayOfWeek == DayOfWeek.Friday) || (DateTime.Now.DayOfWeek == DayOfWeek.Saturday) || (DateTime.Now.DayOfWeek == DayOfWeek.Sunday))
            {
                if (DateTime.Now.ToString("HH:mm") == "23:00")
                {
                    result = true;
                }
            }
    
            return result;
        }
    }

    Create an instance of the class in the class or form to use Decide method.

    private Operations _operations = new Operations();

    Then use Decide as needed e.g. in a click of a button or more likely a Timer 

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_operations.Decide())
        {
            Stop();
        }
        else
        {
            Start();
        }
    }
    You could do similarly in a Windows Service with a System.Threading.Timer which you can start, execute, call your methods, dispose then restart.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by dgadev Friday, January 11, 2019 11:54 PM
    Friday, January 11, 2019 12:33 PM
    Moderator

All replies

  • Hello,

    Perhaps base the decision on HH:MM.

    public class Operations
    {
        public bool Decide()
        {
            var result = false;
            if ((DateTime.Now.DayOfWeek == DayOfWeek.Friday) || (DateTime.Now.DayOfWeek == DayOfWeek.Saturday) || (DateTime.Now.DayOfWeek == DayOfWeek.Sunday))
            {
                if (DateTime.Now.ToString("HH:mm") == "23:00")
                {
                    result = true;
                }
            }
    
            return result;
        }
    }

    Create an instance of the class in the class or form to use Decide method.

    private Operations _operations = new Operations();

    Then use Decide as needed e.g. in a click of a button or more likely a Timer 

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_operations.Decide())
        {
            Stop();
        }
        else
        {
            Start();
        }
    }
    You could do similarly in a Windows Service with a System.Threading.Timer which you can start, execute, call your methods, dispose then restart.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by dgadev Friday, January 11, 2019 11:54 PM
    Friday, January 11, 2019 12:33 PM
    Moderator
  • Is this part of a larger program? What happens when those dates change? It seems like you should just be using task scheduler instead. Create your app to simply do its work when run. Then use task scheduler to schedule the task(s) to run when and for how long you need them to. This also means you can adjust the time/dates without modifying any code.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 11, 2019 3:30 PM
    Moderator
  • Hello Karen,

    What yours code here is do, its returning true Friday, Saturday, Sunday in 23:00 but just when it comes to that time 23:00 which is not really good.

    Friday - 23:00 call Stop();

    Saturday - don't run whole day

    Sunday - don't run until 22:59, 23:00 call Start();

    Try to run it.

    I think i found solution, this can be made like this, but since i am no so good in c# not sure if its its best solution, here is code based on yours example:

    public class Operations
    {
        public bool Decide()
        {
            var result = false;
            var TimeNow = DateTime.Now;
            var Day = DateTime.Now.DayOfWeek;
    
            switch (Day)
            {
                case DayOfWeek.Monday:
                case DayOfWeek.Tuesday:
                case DayOfWeek.Wednesday:
                case DayOfWeek.Thursday:
                    result = false;
                break;
                case DayOfWeek.Friday:
                    if (IsTimeOfDayBetween(Convert.ToDateTime(TimeNow), new TimeSpan(23, 00, 00), new TimeSpan(23, 59, 00)) == true) result = true;
                    else result = false;
                break;
                case DayOfWeek.Saturday:
                    result = true;
                break;
                case DayOfWeek.Sunday:
                    if (IsTimeOfDayBetween(Convert.ToDateTime(TimeNow), new TimeSpan(00, 00, 00), new TimeSpan(22, 59, 00)) == true) result = true;
                    else result = false;
                break;
                default:
                    result = false;
                break;
    
            }
    
            return result;
        }
    
        private bool IsTimeOfDayBetween(DateTime time, TimeSpan startTime, TimeSpan endTime)
        {
            if (endTime == startTime)
            {
                return true;
            }
            return false;
        }
    }

    Usage

    if (_operations.Decide())
    {
       Stop();
    }
    else
    {
       Start();
    }

    This do exactly what i wanted, calling Stop(); method in Friday 23:00:00 and calling Start(); method in Sunday 23:00:00, between those 2 days and times nothing happening.

    • Edited by dgadev Saturday, January 12, 2019 9:17 AM
    Saturday, January 12, 2019 8:44 AM
  • Hello

    My code here need to be based on Days in week and time, not date.

    Something like this 

    Friday - 23:00 call Stop();

    Saturday - don't run whole day

    Sunday - don't run until 22:59, 23:00 call Start();

    Saturday, January 12, 2019 8:49 AM
  • Use task scheduler. Write your app to use a command line option to "start" or "stop". Then use TS to schedule one call to your app every Friday at 2300 to call the program with "stop". Create another task to run every Sunday at 2300 to call "start" again. 

    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, January 12, 2019 9:13 AM
    Moderator