简体   繁体   English

调用非静态方法时出错

[英]Error calling non-static method

I have this timer 我有这个计时器

System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimerMethod);
aTimer.Interval = 5000;
aTimer.Enabled = true;

But when I call this method in timer code, 但是当我在计时器代码中调用此方法时,

private void OnTimerMethod(object sender, ElapsedEventArgs e)
{
    Response.Write("text");
}

I get this error message 我收到此错误消息

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code System.Web.dll中发生类型'System.Web.HttpException'的异常,但未在用户代码中处理

How can I fix this ? 我怎样才能解决这个问题 ?

Update : 更新:
I see I'm doing wrong but how can I achieve doing this ? 我看到自己做错了,但是如何实现呢?

Thanks in advance 提前致谢

I'm trying to make a reminder, so I'll pop some messages at the detected time 我想提醒一下,所以我会在检测到的时间弹出一些消息

You cannot do this by simply writing extra data to a completed response context. 您不能通过简单地将额外的数据写入完整的响应上下文来完成此操作。 The http life cycle doesn't allow for this , at least not in http 1.*. http生命周期不允许这样做 ,至少在http 1. *中不允许 Instead, you would need to (one of): 相反,您将需要(以下一项):

  • move all the timer code to the client and worry about it in javascript 将所有计时器代码移至客户端,并在javascript中担心它
  • run ajax from the client and poll the server periodically 从客户端运行ajax并定期轮询服务器
  • establish a web-socket connection from the client to the server; 建立从客户端到服务器的网络套接字连接; this allows the server to push messages to the client without requiring a request, but requires an established and active TCP connection between client and server, and requires a different programming model for the web-socket server 这允许服务器在不需要请求的情况下将消息推送到客户端,但是需要在客户端和服务器之间建立有效的TCP连接,并且需要针对网络套接字服务器的不同编程模型

Your best bet for a reminder is probably just the first one. 提醒您的最佳选择可能只是第一个。

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

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