如何获得相反顺序的计数?

时间:2012-03-02 04:54:14

标签: c# .net c#-4.0 c#-3.0

我如何以相反的顺序得到计数?现在,我已经显示了计数的数量。但我还想要剩下的计数。如何在下面的程序中每天结束时重置计时器并显示上次执行的时间?有人可以帮我整个程序吗? 该计划是 -

namespace Time_Writer
{
  class Program
  {
    static int count = 1;
    static double seconds;
    private static System.Timers.Timer aTimer;


    static void Main(string[] args)
    {
        ReadCountFromFile();


        aTimer = new System.Timers.Timer();
        aTimer.Elapsed +=new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
        aTimer.Interval = 5000;
        aTimer.Enabled = true;
        Console.WriteLine("Press Enter To Exit The Program\n");
        Console.ReadLine();


        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);


    }
    private static void ReadCountFromFile()
    {
        try
        {
            if (File.Exists(".\\mynumber.dat"))
            {
                using (var file = File.Open(".\\mynumber.dat", FileMode.Open))
                {
                    byte[] bytes = new byte[4];
                    file.Read(bytes, 0, 4);
                    count = BitConverter.ToInt32(bytes, 0);
                    Console.WriteLine("Limit = 10000");
                    Console.WriteLine("Count  = {0}", count);

                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Problem reading file.");
        }
    }
    static void CurrentDomain_ProcessExit(Object sender, EventArgs e)
    {
        using (var file = File.Open(".\\mynumber.dat", FileMode.OpenOrCreate))
        {
            var buffer = BitConverter.GetBytes(count);
            file.Write(buffer, 0, buffer.Length);
        }
    }
    private static void aTimer_Elapsed(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Name is Yap {0}", e.SignalTime);
        seconds += 5;
        count += 1;
        if (count>10000 || seconds==86400)
        {
            aTimer.Enabled = false;
            Console.WriteLine("\n\nTimer is off at {0}\n\n", e.SignalTime.TimeOfDay.ToString());
        }
     }
   }
 }

1 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

Console.WriteLine("Limit = 10000");
Console.WriteLine("Count: {0} of 10000. {1} remaining", count, 10000 - count);