Doubt with Time asp.net mvc

2

I have timer that I'm testing and it returns me an error:

  

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

     

Additional information: Response is not available in this context.

private static System.Timers.Timer timer;

protected void btnGerarArquivo_Click(object sender, EventArgs e)
{
    timer = new System.Timers.Timer();
    timer.Interval = 60;
    timer.Elapsed += new ElapsedEventHandler(Teste);
    timer.Enabled = true;
}

public void Teste(object source, ElapsedEventArgs e)
{
    try
    {
        Response.Write("teste");
        timer.Enabled = false;
    }
    catch (Exception ex)
    {
        Response.Write("erro " + ex.Message);
    }
}

Afterthesettingsgotthisway,mostdonotrefreshthescreen:

System.Threading.TimertTime;protectedvoidbtnGerarArquivo_Click(objectsender,EventArgse){tTime=newSystem.Threading.Timer(newTimerCallback(Teste),null,15000,6000);}publicvoidTeste(objectsender){horaCriacaoPagina.Text="Hora atualizada : " + DateTime.Now.ToLongTimeString();
        }
    
asked by anonymous 05.10.2016 / 22:10

1 answer

0

Your problem is that your project is web and you are trying to use a Timer class that does not support this private static System.Timers.Timer timer ; when you should use System.Web.UI.Timer .

Your error is exactly when you try to use Response, if you use this.Response, you will see that there are several errors in your class and one of them is.

  

'this.Response' threw an exception of type 'System.Web.HttpException'

Remember that the .NET Framework class library includes four Timer classes, each of which offers different functionality:

  • System.Timers.Timer (This topic) : triggers an event at regular intervals. The class is intended for use as a server-based or service component in a multithreaded environment; It has no user interface and is not visible at runtime
  • .
  • System.Threading.Timer: performs a unique callback method in a thread pool at regular intervals. The return method of call is set when the timer is instantiated and can not be changed. Like the System.Timers.Timer class, this class is intended for for use as a server-based or service-based component in a multithreaded environment; it has no user interface and is not visible at runtime.
  • System.Windows.Forms.Timer : one component of Windows Forms that triggers an event at regular intervals. The component has no no user interface and is designed for use in a single thread.
  • System.Web.UI.Timer : An ASP.NET Component That Performs Postbacks of the asynchronous or synchronous web page at regular intervals.
06.10.2016 / 19:45