[英]How to use timer in C#?
I want my function to be executed every 2 seconds to check if there is any new user so that I could make a new log for them. 我希望每2秒执行一次我的函数,以检查是否有任何新用户,以便为他们创建新日志。 This is in console application so i used Thread. 这是在控制台应用程序中,所以我使用了线程。 But my console shut down after it run the function once. 但是我的控制台在运行一次功能后关闭了。 I am expecting the timer to run as long as I am running the console and my createLog function will be executed every 2 seconds. 我希望计时器能够一直运行到控制台,并且我的createLog函数将每2秒执行一次。 I am relatively new to C# so maybe my concept of timer is completely wrong. 我是C#的新手,所以也许我的计时器概念完全错误。 Please help... 请帮忙...
namespace ConsoleApplication1
{
class Program
{
public static Hashtable clientsList = new Hashtable();
static void Main(string[] args)
{
Timer t = new Timer(createLog, null, 0, 2000);
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpListener serverSocket = new TcpListener(ip, 9888);
TcpClient clientSocket = default(TcpClient);
int counter = 0;
serverSocket.Start();
.... //this main is monitoring the network....
console.read();
}
private static void createLog(object state)
{
//this function checks the database and if there is new user it will create a new text file for he/she.
}
}
由于这是一个控制台应用程序,因此您需要添加类似Console.ReadLine()
以使该应用程序保持活动状态,直到用户希望将其关闭。
There are lots of tutorials available over the net ,about how to use the timers.Please go through it and then study your code ,to get the mistake you might have made. 网上有很多关于如何使用计时器的教程。请仔细阅读它,然后研究您的代码,以获取可能犯的错误。 Try this link : http://www.dotnetperls.com/timer 试试这个链接: http : //www.dotnetperls.com/timer
Maybe FluentScheduler can help. 也许FluentScheduler可以提供帮助。
using FluentScheduler;
public class MyRegistry : Registry
{
public MyRegistry()
{
// Schedule an ITask to run at an interval
Schedule<MyTask>().ToRunNow().AndEvery(2).Seconds();
// Schedule a simple task to run at a specific time
Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15);
// Schedule a more complex action to run immediately and on an monthly interval
Schedule(() =>
{
Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
Thread.Sleep(1000);
Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
}).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
}
}
Winforms/Console apps -> Comparing the Timer Classes in the .NET Framework Class Library Winforms /控制台应用程序-> 比较.NET Framework类库中的计时器类
WPF -> Build More Responsive Apps With The Dispatcher WPF-> 使用分派器构建更多响应式应用程序
You need a blocking call like Console.ReadLine
after calling Start
to keep the foreground thread running. 在调用Start
之后,您需要一个类似Console.ReadLine
的阻塞调用来保持前台线程运行。 Also, the your Timer is eligible for garbage collection because it is not referenced again after it is created. 另外,您的计时器有资格进行垃圾回收,因为它在创建后不再被引用。 Use GC.KeepAlive
at the end of the Main method to prevent the timer from being GC'd. 在Main方法的末尾使用GC.KeepAlive
以防止计时器被GC。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.