简体   繁体   English

等待ASPX页面呈现后释放线程?

[英]Wait for ASPX page to render before releasing thread?

I am writing a library which writes something into the database for temporary use (use only once) and then gets removed (its strange, but there is a certain implementation necessary for this ). 我正在编写一个库,该库将某些内容写入数据库以供临时使用(仅使用一次),然后将其删除(这很奇怪,但是为此需要某种实现)。 For this I am trying to execute a thread which writes in the data into the database, waits for the page to finish loading and then releases to finish executing, removing the record. 为此,我试图执行一个线程,该线程将数据写入数据库,等待页面完成加载,然后释放以完成执行,并删除记录。 I am not sure how to apprach this. 我不确定该如何处理。 One way is to make the thread sleep but then I am risking either not allowing enough time or the thread might still be running after the page has finished doing everything else. 一种方法是使线程进入睡眠状态,但是然后我冒着风险,要么没有允许足够的时间,要么在页面完成所有其他操作之后可能仍在运行线程。 Is there a way to tie this into the page thread so I can release my thread right after it finishes? 有没有办法将其绑定到页面线程中,以便在线程完成后立即释放线程? What are some, if any, approaches to something like this? 有什么办法可以解决这样的问题? Because this is a library I would much rather not have another programmer call a function at the end of his page code to remove the record; 因为这是一个库,所以我宁愿没有另一个程序员在其页面代码的末尾调用一个函数来删除记录; its messy and easily forgotten. 它的混乱和容易被遗忘。

Unless you want to use threads for some other reason (ie educational) consider using Application events for that - you can listen AcquireRequestState and ReleaseRequestState or similar pair depending on your needs and perform your database operations. 除非出于某些其他原因(例如,出于教育目的)使用线程,否则请考虑使用Application事件-您可以根据需要监听AcquireRequestStateReleaseRequestState或类似对,并执行数据库操作。 Check application lifecicle to see all events and order of them. 检查应用程序生命周期以查看所有事件及其顺序。

How to: Create ASP.NET Application-Level Event Handlers 如何:创建ASP.NET应用程序级事件处理程序

Why do you create another thread? 为什么要创建另一个线程? Write the record in the Load event and delete it in the Unload event. 将记录写在Load事件中,然后在Unload事件中将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM