简体   繁体   中英

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. Check application lifecicle to see all events and order of them.

How to: Create ASP.NET Application-Level Event Handlers

Why do you create another thread? Write the record in the Load event and delete it in the Unload event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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