Hi all,

I apologise for possibly asking stupid questions and/or overlooking things that I possibly should not be. I'm quite new to this C# and programming in general, but it was forced upon me at university - and after a few lessons it looks something I enjoy and would like to understand a little more.

I'm attempting to create a program which will run in the system tray, and act as a countdown to a recurring event every 36 hours. As I say, this may not be the best way to do it but - I need the countdown to update each time the computer is turned on and the program starts - a countdown to the event. My problem is, I cannot quite get my head around how to set the time it is aiming for! I've used a text file so far to set the original....

I'm at wits end trying to get this to work. I'm damn sure i'm not supposed to have those strings outside hanging around like that, but I was going to worry about it once it worked - perhaps I should just start again!!!

I'd appreciate if anyone could tell me where i'm going wrong here please, I know it's messy, bad and inefficient, but it's just a small learning thing i'm doing that would have hopefully made something useful.

// above this just sends the form down to the system tray unless the menu is used to restore or notifyIcon is dblclick'd.

        String extractedNearestLeveReset; // use in all areas, STRING FROM TXT.
        String shitToPrint;
        DateTime currentLeveTimer; // DATETIME OF STRING FROM TXT.
        Int32 intToCheck;

        private void Main()
        {
            LoadCurrentLeveTime();
            GetTimeString(); 
        }

        private void LoadCurrentLeveTime()
        {
            StreamReader streamReader = new StreamReader(@"c:\MoogleClock\Data\NEARESTLEVERESET.TXT");
            string datetimeNextLeve = streamReader.ReadToEnd();
            streamReader.Close();
            extractedNearestLeveReset = datetimeNextLeve;

// takes the datetime out of the text file and sets it to one of the 'floating' strings and datetimes at the top... 
        }

        public void GetTimeString()
        {
            //Get these values however you like.
            string nearestLeveReset = extractedNearestLeveReset;
            DateTime currentLeveTime = DateTime.Parse(extractedNearestLeveReset); //(string)
            DateTime systemTime = DateTime.Now;
            currentLeveTimer = currentLeveTime;

            //Calculate countdown timer.                
            TimeSpan t = currentLeveTime - systemTime;
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds.", t.Days, t.Hours, t.Minutes, t.Seconds);
            label4.Text = countDown;
            intToCheck = Int32.Parse(countDown);
            Add();
            Main();

        }

//*(DAYS, HOURS, SECONDS, MILLIS) 
        public void Add()
        {
            string loop = "1";
            while (loop == "1")
            {
                if (currentLeveTimer < DateTime.Now)
                {
                    System.DateTime currentLeveReset = currentLeveTimer; // currentLeveReset will now be equal to the extracted datetime from the txt file.
                    System.TimeSpan duration = new System.TimeSpan(0, 36, 0, 0); // add 36 hours to the datetime saved (the event occurs every 36 hrs so always falls on one event :<?
                    System.DateTime NextLeveReset = currentLeveReset.Add(duration); // does the maths, NextLeveReset = the new date (36 hrs after the last!)
                    System.IO.StreamWriter file = new System.IO.StreamWriter(@"c:\MoogleClock\Data\NEARESTLEVERESET.TXT");
                    file.WriteLine(NextLeveReset);                      // writes over the first line in the text file - updates the file, closes.
                    file.Close();
                    Add();                                             // loops back to the top until the date is in the future?
                }
                else
                {
                    break;                    
                }
            }
        }           
    }
}

I need to be able to make a counter from it, if I try to loop the label4.text part it just says it's infinite, even if i've got a break in it! I'm not asking for it to be finished, just advice on how I can get it working once I start again and keep it a little more structured... and possibly a bit better thought out before I begin!

The general gist is;

I'm trying to create a countdown to a certain event every 36 hours - I was trying to check the time of the event in the txt against the system time so when it was passed the program added 36 hours to the last, then rechecked - so on so forth. It reads the text file and works out the difference but no matter what I try I can't make it countdown (trying to refresh the label and re-read the code that updates it.).

Advice for my second attempt would be very welcome!

Thanks in advance.

I've got everything else working, I just need to know how to refresh that timer to re-do the sums and change the string in label4. every second or so (I think!).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.