tags:

views:

223

answers:

5

I have an asp.net page when Loading this page it creates a Thread to do some thing My Question is : How to kill this thread when the user close the page ? I have tried with "onUnload" event but it just works with javascript function (as I know) and we can't use asp.net code in javascript function Do You have a way to help me ... thanks a lot

Edit: It 's difficult to explain what I am Trying to do.. The asp.net page must show a message to the user and this message must appear directly without refreshing so I was trying to use a thread which listen wait the message and then run an AJAX code to show the message ..

+1  A: 

You can't. The browser and the codebehind (the C#) live in different worlds, and once the Page Lifecycle completes (shortly after RenderComplete), the server is done with the request.
It sends the generated HTML to the client and never looks back.

In the onunload event in javascript, you could send an AJAX request to the server to tell it that the user is leaving the page, but that would be highly unreliable, and if you depended on that exclusively to kill threads you would have a major problem on your hands.

Generally, what you're trying to accomplish would be better accomplished another way - so as others have said, let us know what you're trying to do and we'll give suggestions.

Tom Ritter
It 's difficult to explain what I am Trying to doBut I will try ..The asp.net page must show a message to the userand this message must appear directly without refreshing so I was trying to use a thread which listen wait the messageand then run an AJAX code to show the message ..
Hany
In that case you could have the Ajax poll the server periodically.
Mike Brown
You could setup a HttpHandler or Web Service in the same app and have AJAX call that periodically on a client side timer to check for messages to display (as others have recommended). This is a better solution because then no thread needs to be tied up running in a loop.
James
A: 

It is a poor coding practice to start a worker thread that is dependent upon a page closing for its cleanup as you have little control over what happens on the client browser. Perhaps you could re-think the architecture of this, or at least expound on what this worker thread is actually doing so a better solution can be recommended for that.

If the work that the worker thread is set up to do has a clear path to code completion, the garbage collector will handle the cleanup and there is no cause for concern. If your using a thread from the thread pool to perform your background task via the QueueUserWorkItem construct, then the thread will make itself available for use again in the pool once the work is completed, and you have nothing to worry about as long as you ensure proper disposal of unmanaged resources if any.

James
ok I will explain the whole problemmy website is connecting with C# program and we connect the website with the C# program depending ona message Queue C# program will send a message to the message Queuemy page(Thread) is listening to the message queueand when it finds a msg it must show it to the userI hope it's clear enough .. I can explain more if you have problem with this idea
Hany
Could you connect a web service to your C# program so that it can poll the C# program on demand, or does the connection have to the C# program remain open for the entire session?
James
A: 

There is no request sent when the page is closing, so right out of the box, the answer is no. However, you have a few options.

One is to send an XmlHttpRequest (Ajax call) from the javascript onUnload event to a WebMethod.

Another is to execute code at the next beginning of the next pageload, which would cover cases besides those in which the user leaves your site for another or closes their browser.

Finally, there are server-side events you can use for, say, session expiration.

What is it exactly that you're trying to do? Maybe we can help come up with a better way to accomplish it.

JoshJordan
A: 

Just curious: what kind of threaded process are you running? To your question, the only place you're going to be able to capture the event of the browser being closed is in Javascript... you could make an ajax call back to the server to kill whatever thread you've got running for the session, but as others have said, this isn't reliable.

In short there's no direct way to do what you're wanting to do. The best you could hope for is to hook the Sesion_OnEnd event.

Parvenu74
A: 

As Mike pointed out in a comment, you should really set up a web service that some JavaScript on the page can poll periodically - this would then go and interagate the queue, check for messages, and if it finds one, display a message to the user.

This site has a function very similar to that when you are answering a question - it has a "Heartbeat" javascript call that polls the server looking for new answers - if it finds some, an orange message bar appears at the top of the page telling you there are new answers, and would you like to view them?

Zhaph - Ben Duguid