简体   繁体   English

使用计时器每 5 分钟运行一次 Asp.net 代码

[英]Asp.net running code ever 5 min with timer

I get the following error on page_load:我在 page_load 上收到以下错误:

c# no argument given that corresponds to the required formal parameter c#没有给出对应于所需形式参数的参数

Why is this?为什么是这样?

protected void Page_Load(object sender, EventArgs e)
{
    timerevent();
}

DataSet _dataSet = new DataSet();
private DataTable _dataTable; 

string _sqlQuery = @"show slave status;";
string _sqlStop = @"stop slave;";
string _sqlStart = @"start slave;";
System.Timers.Timer mytimer = new System.Timers.Timer();


private void SetTimer()
{
    mytimer.Elapsed += new ElapsedEventHandler(timerevent);
    mytimer.Interval = 1000;
}

private void timerevent(object sender, ElapsedEventArgs e)
{
    BindData();
}

public void BindData()
{
    mytimer.Start();
}

Did you stop to consider the lifecycle of a Page?你有没有停下来考虑一下页面的生命周期? Think about it.想想看。

An instance of your Page class is created when an HTTP request comes in. Once the page has gone through all of its lifecycle events, an HTTP response is sent to the client.当 HTTP 请求传入时,会创建 Page 类的一个实例。一旦页面经历了其所有生命周期事件,就会向客户端发送 HTTP 响应。 At that point, the Page instance will be destroyed.此时,Page 实例将被销毁。 It will go away, taking any state stored in it (such as field containing a timer) with it.它会消失,带走存储在其中的任何状态(例如包含计时器的字段)。

For these reasons, the approach you're trying simply will not work.由于这些原因,您尝试的方法根本行不通。

If you want to update the client's DOM update every 5 minutes, you'll either have to have some client side code (JavaScript) that contains a timer and initiates a new HTTP request, or use tech like web sockets ( SignalR ) to push changes.如果您想每 5 分钟更新一次客户端的 DOM 更新,您要么必须拥有一些包含计时器并启动新 HTTP 请求的客户端代码 (JavaScript),要么使用网络套接字 ( SignalR ) 之类的技术来推送更改. If you do choose a SignalR approach, you can have a separate service with scheduling functionality (perhaps using Hangfire or Quartz ) to initiate the push.如果您确实选择了 SignalR 方法,您可以使用具有调度功能的单独服务(可能使用HangfireQuartz )来启动推送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM