2

I have created one-timer. After timer invokes I need to redirect to another page.

Please help me.

My Code :

System.Timers.Timer myTimerfortodolist;
void Application_Start(object sender, EventArgs e)
{

    myTimerfortodolist = new System.Timers.Timer();
    myTimerfortodolist.Interval = 60000;//86400000 milisecond is equal to 24;
    myTimerfortodolist.AutoReset = true;
    myTimerfortodolist.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_TODO);
    myTimerfortodolist.Enabled = true;

}


//create for check send Interval from Reminder Config within 5 minute
public void myTimer_TODO(object source, System.Timers.ElapsedEventArgs e)
{
        if (//Condition//)
        { 
          Response.Redirect("~/Authorize.aspx");
        }

}

Response is not available in this context.

I am Redirecting from Page to another and timer is running at each 1 min so I am unable to get Current HttpContext in delegate method

Arvind Agrahari
  • 315
  • 1
  • 3
  • 16
  • TRY `HttpContext.Current.Response`. Also not sure that at `Application_Start` you can get `Context`. Have a look on [this link](http://stackoverflow.com/questions/2518057/request-is-not-available-in-this-context) – शेखर Jun 09 '16 at 10:35
  • HttpContext.Current having null value in myTimer_TODO thread – Arvind Agrahari Jun 09 '16 at 10:51
  • Can't comment. Here is the answer -> http://stackoverflow.com/a/9977501 – Andrey Bobrov Jun 09 '16 at 11:02
  • 1
    Try using System.Threading.Timer than System.Timers.Timer , System.Threading.Timer class makes callbacks on a ThreadPool thread and does not use the event model https://msdn.microsoft.com/en-us/library/zdzx8wx8.aspx – Amit Pore Jun 09 '16 at 11:07
  • 1
    In the above case , System.Timers.Timer elapse on a separate thread than the thread that created. – Amit Pore Jun 09 '16 at 11:09
  • 1
    @ArvindAgrahari Edit doesn't make any sense i am afraid – Amit Pore Jun 09 '16 at 11:24
  • @AmitPore I'm checking on every minute if condition is fulfilled then redirect to another page – Arvind Agrahari Jun 09 '16 at 11:38
  • is any way to get HttpContext.Current. – Arvind Agrahari Jun 09 '16 at 11:41
  • @adb It still giving null when I used to pass HttpContext.current as third variable. – Arvind Agrahari Jun 09 '16 at 11:44
  • 1
    @ArvindAgrahari ... You might not understand well `asp.net life-cycle` ... it's not `winforms or wpf application` .... where you can do something like that very easily ... If you share the whole concept .. why you want this type of code to execute .. than might be we can help you .. – Moumit Jun 09 '16 at 11:48

1 Answers1

1

Why you putting so much effort .. when you have a simple solution for that .. Use Thread.sleep()

void Application_Start(object sender, EventArgs e)
{
    System.Threading.Thread.Sleep(60000);//86400000 milisecond is equal to 24;
    Response.Redirect("~/Authorize.aspx");
}

Edit: Response will be null because httpcontext is null that time.Why? Because when you create and set timer the thread handling it is still alive in server to process your code ... but by that time server already processed the httprequestcontext and responded back .. so there is no more request and response or session is accessible ... they get created only when server had to process a HttpRequest call ...

Moumit
  • 8,314
  • 9
  • 55
  • 59