A way to control the code flow of an asp.net while it's running

A

Andrea Moro

I do a stupid thing ... new webform, in the page_load event added this code

dim i as int64

do while true
i = i + 1
loop

run the project, stop adfter a while ... and then putted a breakpoink on
the middle line (i = ...). With surprise I discovered that code continue to
run also if an end request was generated by the user, and seem no way to
avoid it.

I added a button with a cancel variable, that check before increment i, but
I can press the button many time, but cancel never became true.

What am I missing?

Thanks
Andrea
 
N

Norman Yuan

Let's what was happening:

1. you start the project: under the hood, it is like a browser starts on
your computer as web app client.
2. on the server (even it is the same computer), that page gets processed by
IIS/ASP.NET engine: Page_Load() routine runs you cute indefinite loop, so
the Page_Load() routine will never finish until the computer dies.
3. in the Browser, you get a blank window, because the page loading would
never finish.
4. you close the Browser (there is not "end request" being sent to server
when you close browser!), but the server has no knowledge on user's clossing
browser. The loop continues.

In order to interrupt a loop, you need to put some logic in the loop so that
the process can check particular condition per loop or per several loop to
decide whether to quit or not. In your case, since you put loop in
Page_Load(), so that the page will never be sent to user's browser, the user
will never be able to cancel it, no matter what kind of cancelling logic you
have.
 
K

Karl Seguin

When you first make a request, Page_Load fires....

if you hit "stop" in the browser, or a "cancel" button, the thread which was
given the first request will still be in an endless loop until the thread
timesout...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
A

Andrea Moro

When you first make a request, Page_Load fires....

if you hit "stop" in the browser, or a "cancel" button, the thread which was
given the first request will still be in an endless loop until the thread
timesout...

Karl

But I tried to add a button and click to run the code after page was sent
to the browser. For instance, page_load shows me the button and whatever
was in the page, click the button ... it runs, click the cancel never stop.

So I don't think that problem is on the page load. Is that it cannot get my
cancel request.

Andrea
 
A

Andrea Moro

As I said to Karl's post, I tried to add a start button, but when I was
canceling pressing it down, cancel variable never changes value and cycle
never stopped.

Andrea
 
B

Bruce Barker

you are missing web process logic

browser request page -->

server receives the request
fire onint
fire onload
fire any control events
fire unload
<---send response (page html) back to browser

browser renders new page

if you put a loop in inload, the browser will never see the new html. if you
click cancel in the browser, it end the old request (but the server will not
know until it writes back. then the browser sends a a new request to the
server, which starts a new thread to process the cancel request (which in
turn will hang on onload).

unlike window application, onload fires even on a button click (browser page
request), because a new page (html response) is built. a new form is built
and destroyed on every page request or button click. this means you cannot
use background threads like in a windows app.


-- bruce (sqlwork.com)
 
A

Andrea Moro

So what can I do to perform a long operation and be able to suppress it,
whituout the w3pce.exe service continue to run and do the previously job
started?

Andrea
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top