Doubt with Time Asp.net mvc

Asked

Viewed 471 times

3

I have a timer that I am testing and the same 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: Answer 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);
    }
}

inserir a descrição da imagem aqui

After the adjustments was this way, it no longer updates the screen:

     System.Threading.Timer tTime;

        protected void btnGerarArquivo_Click(object sender, EventArgs e)
        {
            tTime = new System.Threading.Timer(new TimerCallback(Teste), null, 15000, 6000);
        }



        public void Teste(object sender)
        {
            horaCriacaoPagina.Text = "Hora atualizada : " + DateTime.Now.ToLongTimeString();
        }
  • 1

    You have to have more details of the bug, young man. Probably Exception doesn’t just say that.

  • @jbueno, I have only the content presented, added a Try catch, see if help

  • 1

    You need to get the contents of InnerException. Adding a Try-catch will only make things worse, instead of having the entire Exception it will only return the message of the first exception, this is terrible. By the way, why do you put your code as lang-html? This is C#, the syntax highlighting is impaired if you use the comment with lang-html.

  • @jbueno, I added the image, I used lang-html, because for me it is more practical to adjust the code.

  • 1

    Your project is Web?

  • yes, it’s web thanks for the help

  • And you want to generate a file?

  • actually I want to call a procedure with a time interval, I did this example to try to figure out why of the error.

  • I think it’s cool that you edit and take these Response.Write, it gets a little confusing

  • I made an adjustment to the question, I am using System.Threading.Timer tTime; more does not update the result on the screen

Show 5 more comments

1 answer

0

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

Your mistake 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 classes called Timer, each of which offers different functionality:

  • System.Timers.Timer (This topic): fires an event at regular intervals. The class is intended for use as a server based on or service component in a multithreaded environment; it has no user interface and is not visible at runtime.
  • System.Threading.Timer: performs a single call return method in a thread pool at regular intervals. The return method of call is set when timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for for use as a server-based component or service in a multithreaded environment; it has no user interface and is not visible at runtime.
  • System.Windows.Forms.Timer: a component of Windows Forms that fires an event at regular intervals. The component does not have no user interface and is designed for use in a single thread.
  • System.Web.UI.Timer: an ASP.NET component that performs postbacks asynchronous or synchronous web page at regular intervals.
  • I made an adjustment to the question, I am using System.Threading.Timer tTime; more does not update the result on the screen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.