我想請問一下,我已經寫在 主要程式(Main),想要他做完之後,每小時再重新執行(Main)。
我找到的計時器那些全部都是以呼叫來做執行,就像以下
    static void Main(string[] args)
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        timer.Enabled = true;
        timer.Interval = 600000; //執行間隔時間,單位為毫秒; 這裡實際間隔為10分鐘  
        timer.Start();
        timer.Elapsed += new System.Timers.ElapsedEventHandler(test); 
        Console.ReadKey();
    }
    private static void test(object source, ElapsedEventArgs e)
    {
          Console.WriteLine("OK, test event is fired at: " + DateTime.Now.ToString());
       
    }
}
}
我想請問有沒有方法,可以讓主程式,每小時重新執行,還是我只能把我寫在Main的改成呼叫方式?
 static void Main(string[] args)
 {
    Console.WriteLine("以下是我寫的程式");
               .
               .                   
    Console.WriteLine("執行中");
               .
               .
    Console.WriteLine("執行完成");
    Console.WriteLine("一小時後重新執行");
 }
要完成您說的方法,最簡單的方式就是用Thread.Sleep,
但我還是建議用Windows排程定期執行,
沒有必要無時無刻都卡一條Thread吧~XDDD
// Example來自官網
class Example
{
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Sleep for 2 seconds.");
            Thread.Sleep(2000);
        }
        Console.WriteLine("Main thread exits.");
    }
}
依我目前的做法,都是直接用工作排程器。
如果要改執行頻率,程式一行都不用改,直接改排程器就好。
簡單方便,彈性又大。
using System.Threading;
void main(){
    DateTime lastdt = DateTime.Now
    while(true){
        if((DateTime.Now - lastdt).TotalHours >= 1){
            lastdt = DateTime.Now;
            // ........ 這裡放你要執行的工作
        }
        Thread.Sleep(1); //睡一秒
    }
}
不過最好的方法就是掛排程啦,跑這種程式,再怎樣也是佔一個Process..