timer ability in serverside c#, triggered from aspx?

  • Thread starter Thread starter Jason Shohet
  • Start date Start date
J

Jason Shohet

There's common use of setTimeout() in javascript to cause a delay on the
client of an asp.net app.
I'm experimenting w/ a serverside delay. Ie, maybe there's a backend
process that I want to occur 10 seconds after the user hits the button on
the webpage -- not immediatly. Perhaps 1 codebehind function to call
another codebehind function after a 10 second delay. Any ideas if this can
be done?
 
it seems System.Threading.Thread.Speep(10000); would cause a 10 sec delay.
not sure if its the best way to do it but it seems to work
 
Jason,

Why you would want to do that is beyond me (then again, what do I
know?).

The only way I can think of doing this is to call Sleep on the current
thread. You would have to put the thread on hold somehow (if you exit the
processing of the thread, then the response is sent back). The best way to
do this would be through a call to Sleep.

Hope this helps.
 
Jason said:
There's common use of setTimeout() in javascript to cause a delay on
the client of an asp.net app.
I'm experimenting w/ a serverside delay. Ie, maybe there's a backend
process that I want to occur 10 seconds after the user hits the
button on the webpage -- not immediatly. Perhaps 1 codebehind
function to call another codebehind function after a 10 second delay.
Any ideas if this can be done?

Maybe you can use the ThreadPool class for this.
One thing you *can't* do is create a Timer in the aspx to execute
a function "in a little while", because that instance of the aspx-class
will cease to exist when the request is done.
You might be ably to use "Sleep" (as other posters replied), but that has
the disadvantage that the page "hangs" during this time and request-timeouts
may occur.

Hans Kesting
 
this is curiosity only here, but lets say you do:

Thread.Sleep(some #);
some code to write to a text file;

Lets say the session expires before the amount of time. Will the code that
writes to a text file execute. IOTW, does the session expiring also kill
the thread.
 
Jason said:
this is curiosity only here, but lets say you do:

Thread.Sleep(some #);
some code to write to a text file;

Lets say the session expires before the amount of time. Will the
code that writes to a text file execute. IOTW, does the session
expiring also kill the thread.

I don't know, never needed to find out.
The user requesting the original page would probably see a
"hanging site" as the page never finishes processing/loading.
Maybe a Response.Flush() can send the page contents (so far)
to the browser, but that browser will still wait for "the rest".

I don't think it is generally a good idea to use Sleep in
a website.

Hans Kesting
 
Back
Top