如何关闭浏览器关闭事件的所有线程?

时间:2021-09-20 11:06:16

I have website which runs multiple threads. When user close the browser but threads are still running. How to kill/stop all thread in asp.net on browser close.

我有运行多个线程的网站。当用户关闭浏览器但线程仍在运行时。如何在浏览器关闭时杀死/停止asp.net中的所有线程。

2 个解决方案

#1


3  

There is no message sent to the server when the browser closes so there's no preemptive event to listen to. What you'll want to do is make a variable available to your threads that indicates when the user last requested a resource from your server. Once a certain amount of time has passed since the user last requested expires you end the threads. Then, in your HTML add a javascript ajax request that pings your server to keep the threads alive.

浏览器关闭时没有消息发送到服务器,因此没有抢先的事件要收听。您要做的是为线程提供一个变量,指示用户上次从服务器请求资源的时间。自用户上次请求到期后经过一定时间后,您将结束线程。然后,在您的HTML中添加一个javascript ajax请求,ping您的服务器以保持线程活动。

Obviously you'll need to tweak the timeouts to balance performance and reliability for your users.

显然,您需要调整超时以平衡用户的性能和可靠性。

#2


2  

You could have a KillThreads action in the server and call it with AJAX when you detect the user is attempting to close the browser in the client.

您可以在服务器中执行KillThreads操作,并在检测到用户尝试关闭客户端中的浏览器时使用AJAX调用它。

#1


3  

There is no message sent to the server when the browser closes so there's no preemptive event to listen to. What you'll want to do is make a variable available to your threads that indicates when the user last requested a resource from your server. Once a certain amount of time has passed since the user last requested expires you end the threads. Then, in your HTML add a javascript ajax request that pings your server to keep the threads alive.

浏览器关闭时没有消息发送到服务器,因此没有抢先的事件要收听。您要做的是为线程提供一个变量,指示用户上次从服务器请求资源的时间。自用户上次请求到期后经过一定时间后,您将结束线程。然后,在您的HTML中添加一个javascript ajax请求,ping您的服务器以保持线程活动。

Obviously you'll need to tweak the timeouts to balance performance and reliability for your users.

显然,您需要调整超时以平衡用户的性能和可靠性。

#2


2  

You could have a KillThreads action in the server and call it with AJAX when you detect the user is attempting to close the browser in the client.

您可以在服务器中执行KillThreads操作,并在检测到用户尝试关闭客户端中的浏览器时使用AJAX调用它。