Arvind Agrahari
Arvind Agrahari

Reputation: 315

How do I access HttpContext.Current in Another thread

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

Upvotes: 2

Views: 790

Answers (1)

Moumit
Moumit

Reputation: 9510

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 ...

Upvotes: 1

Related Questions