如何在指定时间执行指定的任务

mss*_*ssb 4 c#

我是C#的新手,但是java有在指定时间执行指定任务的方法,所以使用c#它是怎么做的

Timer t=new Timer();
TimerTask task1 =new TimerTask()           

t.schedule(task1, 3000);
Run Code Online (Sandbox Code Playgroud)

bas*_*rat 7

您可以在此处获得有关计时器如何在C#中运行的完整教程:http://www.dotnetperls.com/timer

简而言之:

using System;
using System.Collections.Generic;
using System.Timers;

public static class TimerExample // In App_Code folder
{
    static Timer _timer; // From System.Timers
    static List<DateTime> _l; // Stores timer results
    public static List<DateTime> DateList // Gets the results
    {
        get
        {
            if (_l == null) // Lazily initialize the timer
            {
                Start(); // Start the timer
            }
            return _l; // Return the list of dates
        }
    }
    static void Start()
    {
        _l = new List<DateTime>(); // Allocate the list
        _timer = new Timer(3000); // Set up the timer for 3 seconds
        //
        // Type "_timer.Elapsed += " and press tab twice.
        //
        _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
        _timer.Enabled = true; // Enable it
    }
    static void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        _l.Add(DateTime.Now); // Add date on each timer event
    }
}
Run Code Online (Sandbox Code Playgroud)