Re: Safe multithreading in ASP.Net

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Rick Strahl [MVP] (rickstrahl_at_hotmail.com)
Date: 08/06/04


Date: Fri, 6 Aug 2004 02:57:11 -0700

Hi Chris,

Why is your thread running in this endless loop? Shouldn't there be some
logic to terminate the loop in and of itself? unless you're planning on
creating some sort of pool manager for your thread(s) there needs to be
self-destruct logic in your threads. If you do have some such mechanism you
can use a static property on some global (Global works) or static property
that can get triggered by Application shutdowns.

As others have said here I'd be very wary of firing threads out of ASP.Net
unless you know exactly what you're doign. If everything's self contained
and doesn't rely on anything of the page (which usually means copyin what
you need to a new object), then running additional threads is safe. But you
still need an exit strategy of some sort. Do what you need to do - slow or
otherwise and get out!

The other thing that you can do is look into async Web requests, which are
relatively easy to set up and run. But there's really no huge benefit for
this over just tieing up an ASP.Net thread either in the first place.

+++ Rick ---

-- 
Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web
"who be dat?" <whatever@dot.com> wrote in message
news:10h5vp3hujp9452@corp.supernews.com...
> Consider the following code which enables multithreading in an ASP.Net
> application I'm writing:
>
> Code in global.asx Application_start subroutine, Threadnotify is declared
> Globally as a new thread where it uses the address of EmailNotify below:
>
> ThreadNotify.IsBackground = True
>
> ThreadNotify.Start()
>
> --------
>
> Public Sub EmailNotify()
>
>   Dim emails As New ForumEmail
>
>   Do While ContinueNotify   'boolean value set to True so loop will
continue
>
>     emails.EmailNotify()
>
>     Thread.Sleep(notifyinterval * 60000)   'notifyinterval is set via app
> settings in web.config file
>
>   Loop
>
> End Sub
>
> --------------------
>
> From global.asx file in Application_End subroutine
>
> ThreadNotify.Abort()
>
>
>
> As you can probably tell, what happens is Threadnotify is started when the
> application starts.  The subroutine simply loops through and calls a
> subroutine then it pauses in intervals of minutes.  Good news: this works.
> Bad news: this works too good.  Problem is, when does this thread get
> terminated?  I can set the boolean notify in the loop to false so the
thread
> will reach the end of it's code and it will terminate.  However, beyond
this
> I have concerns.  I'm writing an application on my machine and testing it.
> I can't get that thread to stop unless I set the boolean value to false in
> that loop which is bad.  When I end the debug execution of the app, the
> thread continues and it doesn't stop.  Heck, I can stop the IIS Service
and
> the thread still continues to execute.  How do I know it's executing.  The
> goal of this thread is to send emails.  I'm getting emails indicating
which
> tells me this thread is still running.
>
> This leads to a larger issue.  When I release this application for others
to
> use, how can I ensure this thread won't keep running even though a user
> might end the web service?  This sucker just keeps going and going.
>
> Suppose I upload this to a production webserver.  The thread starts and
> executes fine.  Suppose I make some changes to the application code and
copy
> the necessary files to the production webserver.  Will the thread from the
> previous versoin of the website continue while a new thread is started by
> the updated code i.e. there will be two threads doing the same thing?
>
> The way this thread runs, I believe I can stop the webservice and destroy
> the webapplication.  The thread would still run.  This isn't acceptable.
> How do I ensure this thread stops?
>
> What is this thread running under?  I would've thought the thread would
run
> under IIS and consider IIS to be the parent process.  However, I'm
> terminating IIS and this thread still keeps going.  What is it running
> under?
>
> Ultimately, when the heck does this thread terminate?!  The only thing I
can
> do to stop this thread is to reboot the machine!  I'm scared to publish
this
> to my website as it may start up and never stop until they reboot their
> machines.
>
> This thread is supposed to send emails after a pause determined by the
user.
> Is there another way to do this?  I though about using a timer that, when
X
> amount of minutes passed, then the event would fire.  I could capture that
> event and execute the email code. I wasn't sure how to do this though.
> Suggestions?
>
>
>
> Thanks
>
> Chris Smith
>
>


Relevant Pages

  • Safe multithreading in ASP.Net
    ... Code in global.asx Application_start subroutine, ... Dim emails As New ForumEmail ... Do While ContinueNotify 'boolean value set to True so loop will continue ... the thread still continues to execute. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Program analysis
    ... known if a program will ever terminate. ... an effect on the loop condition. ... would/would not terminate you might have resolved FLT without all the 'fancy ... the Halting Problem is about programs in general: ...
    (comp.programming)
  • Re: A Good Use of End
    ... I know it isn't "the done thing" for example to write code that runs in a loop when a Timer would be more suitable, but many people do it, especially when writing applications that produce animated graphic output of some sort and it is a fairly commonly accepted practice. ... Private Sub Command1_Click ... If you click Command1 to start the loop and then click Command2 while the loop is running then on most systems the program will not actually finish. ... If ytou're running it as a compiled exe it will certainly *appear to terminate*, but it'll still be hanging on in there if you look at the lost of running apps. ...
    (comp.lang.basic.visual.misc)
  • Re: Ada and (SIGTERM?)
    ... signal and terminate gracefully: did you look at RM C.3? ... me you could set up an interrupt handler to catch SIGTERM and have it ... the variable), then instead of an "infinite" loop, make the loop ... could set a Boolean in the protected object that controls a protected ...
    (comp.lang.ada)
  • Re: Any way to force an iteration in a For Each loop?
    ... >Is there some way to terminate one iteration in a For Each loop ... >For Each obChar In Selection.Characters ... If obChar.Text = "A" Then GoTo SkipIt ...
    (microsoft.public.word.vba.general)