简体   繁体   中英

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

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.*. Instead, you would need to (one of):

  • move all the timer code to the client and worry about it in javascript
  • run ajax from the client and poll the server periodically
  • 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

Your best bet for a reminder is probably just the first one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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