Reputation: 28741
I have a web application that generates enlarged images of smaller images uploaded by other team and stops if no image is left in uploaded folder.
A button needs to be clicked to start generating image and restart every time it finishes.
Can this click event be fired automatically at regular interval say at 15 minutes or better still just as the application stops.
A similar question was asked on earlier thread How to write C# Scheduler .
private void OnTimerTick()
{
if (DateTime.Now.Minutes % 15 == 0)
{
// Do something
}
}
Where should I place timer to call below code ?
protected void btnImgGenerate_click()
{
// Application code
}
Is there a way to check whether a web application in asp.net has stopped executing and then restart it automatically?
Using timer, I can schedule application to start and stop at specific time of day and keep application running throughout specified duration of day using second method.
Upvotes: 0
Views: 5393
Reputation: 22448
You can add code to initialize timer from the System.Threading namespace in Global.asax file and configure it to execute some functionality with desired periodicy. But if your application crashes you'll get following issue:
Is there a way to check whether a web application in asp.net has stopped executing and then restart it automatically ?
Using timer , I can schedule application to start and stop at specific time of day and keep application running throughout specified duration of day using second method .
Nope. You can't start web appliccation from itself if it stopped by some reasons and no new requests comes. So in my opinion for your purpose better suited a windows service.
Upvotes: 1
Reputation: 4962
Have a Button click event that starts the timer-
protected void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
and in the timer Tick event, you can do something like this-
protected void timer1_Tick(object sender, EventArgs e)
{
timer1.enabled=false;
//do your coding
timer1.enabled= true;
}
So once you click the button the timer starts, does all the operations and at the end restarts automatically, after you set timer1.enabled to true.
If you are looking for automating this without the click event, you can use the javascript setInterval() Method- See the example here-
http://www.w3schools.com/jsref/met_win_setinterval.asp
Upvotes: 0
Reputation: 46628
A jQuery implementation for clicking a button after 15 minutes (which will cause a postback and trigger your event) is:
$(document).ready(function(){
setTimeout(function(){$('btnImgGenerate').click();},900000);
});
Upvotes: 1