0

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.

2
  • Have a look at this, it may help you: msdn.microsoft.com/en-us/library/ms178472.aspx
    – Allov
    Jul 2, 2012 at 15:56
  • few things you shouldn't use custom threads in asp.net application so i suggest you to insert/update db onload then delete onunload every thing in win service
    – hackp0int
    Jul 2, 2012 at 18:52

2 Answers 2

1

Unless you want to use threads for some other reason (i.e. 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

-1

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

2
  • " 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." Jul 2, 2012 at 16:56
  • You can create a BasePage class and handle the record in the load and unload events. You only have to force the library user to inherit your class, although he might not like it. Jul 3, 2012 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.