agnieszka
agnieszka

Reputation: 15345

How to set a timer in asp.net

I have a page where when some operation goes wrong i want to start the timer, wait 30 second, stop the timer and retry the operation. Every time the timer starts I need to inform the user about it by changing some label's text.

How can I do it?

Upvotes: 1

Views: 13307

Answers (3)

vasmos
vasmos

Reputation: 2586

Two ways to do this, first way,a bit better if you need to call other functions on the same thread. Add a ScriptManager and a Timer to the aspx page, you can drop from toolbox or simply enter the code. ScriptManager must be declared before asp:Timer. OnTick fires after every interval.

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" Interval="4000" OnTick="Timer1_Tick">
    </asp:Timer>

In the code behind (c# in this case):

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("tick tock");
    }

Second way not as good if you need the functions to fire on the same thread. You can do a timer in ASP.net using C#, the following code fires the function every 2 seconds. In the code behind (.cs) file:

    // timer variable
    private static System.Timers.Timer aTimer;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;
    }

Then make the function you want to call in this format:

//Doesn't need to be static if calling other non static functions

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }

Sample Output:

Output

Upvotes: 0

M4N
M4N

Reputation: 96541

If I understand correctly, the I think you should use a client-side (javascript) timer instead. You can not use a server-side timer for this.

When you detect the error-condition, you update the label accordingly and display it to the user. At the same time you invoke a client-side timer which will postback after 30 seconds.

E.g. put the following timer-code onto your page:

  <script>
    function StartTimer()
    {
      setTimeout('DoPostBack()', 30000); // call DoPostBack in 30 seconds
    }
    function DoPostBack()
    {
      __doPostBack(); // invoke the postback
    }
  </script>

In case of the error-condition, you have to make sure that the client-side timer will be started:

if (error.Code == tooManyClientsErrorCode)
{
  // add some javascript that will call StartTimer() on the client
  ClientScript.RegisterClientScriptBlock(this.GetType(), "timer", "StartTimer();", true);
  //...
}

I hope this helps (the code is not tested, since I don't have visual studio available right now).

Update:

To "simulate" a button click, you have to pass to button's client id to the __doPostBack() method, e.g:

function DoPostBack()
{
  var buttonClientId = '<%= myButton.ClientID %>';
  __doPostBack(buttonClientId, ''); // simulate a button click
}

For some other possibilities, see the following question/answer:

Upvotes: 4

keithwarren7
keithwarren7

Reputation: 14280

from the client side to force a postback you can directly call the __doPostBack method

It takes two arguments, EVENTTARGET and EVENTARGUMENT; since you are making this call outside the normal asp.net cycle you will need to check IsPostBack on your page load event (or init, your choice) - if it is a postback then you will need to look at those two argument which get rolled up as form elements (Request.Form["__EVENTTARGET"]). Check their value to see if the postback came from your call or one of the other controls, if the value of those matches what you pass in from the client side then make your change to the label test

Upvotes: 1

Related Questions