定期使用Timer / Thread调用方法-假设我需要每三分钟自动调用一个方法

时间:2019-01-24 13:36:37

标签: c# multithreading timer

我想从Program类的Main调用工作流控制器类的Run方法,以验证文件处理正在进行或每3分钟间隔挂起。我正在寻找帮助,例如可以使用的线程或计时器。使用计时器/线程哪个好?

class Program
{
    static Workflow wf = null;
    static void Main(string[] args)
    {
        List<string> files = new List<string> { "A1", "A2" , "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12", "A13", "A14", "A15", "A16", "A17" };

        foreach(string file in files)
        {
            wf = new Workflow(file, DateTime.Now.ToString());
            Console.WriteLine("Processing : " + file);
            Thread.Sleep(20000);
            Console.WriteLine("Processed : " + file);
        }
    }
}
class Workflow
{
    public string FileName;
    public string TimeStamp;
    public Workflow(string FileNameP, string TimeStampP)
    {
        FileName = FileNameP;
        TimeStamp = TimeStampP;
    }
}
class WorkflowController
{
    string LastFileName = string.Empty;
    string LastTimeStamp = string.Empty;

    public void Run(Workflow wf)
    {
        if(wf!=null)
        {
            if(string.IsNullOrEmpty(LastFileName) || string.IsNullOrEmpty(LastTimeStamp))
            {
                LastFileName = wf.FileName;
                LastTimeStamp = wf.TimeStamp;
            }
            else if(LastFileName.Equals(wf.FileName) && LastTimeStamp.Equals(wf.TimeStamp))
            {
                Console.WriteLine("Hung");
            }
            else
            {
                LastFileName = wf.FileName;
                LastTimeStamp = wf.TimeStamp;
            }
        }
    }
}

0 个答案:

没有答案