2

如何让我的 ftp 脚本每 10 或 30 秒运行一次?

ftp脚本:

FtpWebRequest makedir = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx/" + System.Environment.MachineName + "___" + System.Environment.UserName + @"/");
makedir.Method = WebRequestMethods.Ftp.MakeDirectory;
makedir.Credentials = new NetworkCredential("usr", @"passwd");
FtpWebResponse makedirStream = (FtpWebResponse)makedir.GetResponse();
makedirStream.Close();

我正在阅读有关在线程上使用睡眠以及使用计时器的信息。但我不知道如何使用它们。问题是它还需要每 30 秒重新运行一次,而不仅仅是一次。

4

1 回答 1

3

Put your code in a method named like RunFtp() then use a Timer like this:

var t = new System.Threading.Timer(o => RunFtp(), null, 0, 30000);

Or use Windows Task Scheduler to schedule and run the application repeatedly.

Or using the Sleep() method like this...

while (true)
{
    RunFtp();
    System.Threading.Thread.Sleep(30000);
}

...will cause your code to pause for 30 seconds between executions, but you also need to add the execution time of your code. So it will not start running every 30 seconds accurately.

于 2013-05-05T20:15:56.813 回答