3

First, thanks for all the replies!

I want to be more specific - I have a website that shows some current and historical reports. I want to be able to allow users to delete all or some of the history, while still navigating the website.

Therefore, I want to run a separate thread that will handle deleting the data, but I want to give this thread a low priority so it doesn't make the web site slow or unresponsive.

I'm in the design phase right now and I'd appreciate some strategy suggestions. Thanks!

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
Sharone Shani
  • 31
  • 1
  • 4
  • Won't help you with memory in the slightest, and it does have some serious down-sides, though not applicable to everything else. What is the rest of the application doing, could this perhaps be an entirely separate app? How many cores do you have, and does this stay at 100% total CPU or just 100% of one of the CPUs? – Jon Hanna Sep 05 '12 at 15:40
  • Are you using a database? If not, sounds like you should be heading that way. – Martin James Sep 11 '12 at 08:11

3 Answers3

2

You should be fine. Lowering the priority of CPU-intensive background tasks to allow 'normal' response from a GUI and/or other apps is one of the better uses for altering thread priorities.

Note that lowering the thread priority is not going to have an enormous efect on any resource except CPU, but still worth doing IME.

If you want to carry on using Firefox, Office, VLC etc. while running your app then, yes, lower the priority of the CPU-heavy threads. You should then be able to browse SO, listen to some albums or watch a few films while waiting for your results to eventually come out :)

Martin James
  • 24,453
  • 3
  • 36
  • 60
1

Note that if you want to change the priority for a thread, you should make sure it's one you've created yourself. It's not good to change the priority on a thread pool thread. This means avoiding the new Task features, unless you're prepared to write a TaskScheduler that doesn't use the thread pool.

Also consider setting the process priority instead, if that suits your scenario. See MSDN for more info. This would affect threads equally.

Edit: Thanks for the additional information. It sounds as though your code is hosted in IIS. From this answer we can confirm that IIS uses the same thread pool as ThreadPool.QueueUserWorkItem - the standard .NET thread pool. Therefore you must not alter the priority of a thread pool thread; these threads belong to IIS.

You could create your own thread. But it seems ill advised to try to host a background operation in IIS like this. You never know when the app pool might be recycled, for example.

  • It would be better to consider a couple of other options. The best solution for a potentially long running background operation seems to be workflow services. Used in conjunction with AppFabric Server, these are very powerful and sound as though they would handle your situation.
  • A simpler alternative would be to move the process outside of IIS. Maybe the user's action could mark items for deletion, then a scheduled task outside of IIS could run to perform the slow operation.
Community
  • 1
  • 1
Olly
  • 5,966
  • 31
  • 60
  • Why is it not a good idea? If you're the only one using the pool, why not? That said, I don't know how to do such a thing efficiently - the priority really only needs to be set once, so where do you do it? – Martin James Sep 05 '12 at 15:34
  • Well, you do have a point. Before your long-running code you can put `Thread.CurrentThread.Priority = ThreadPriority.BelowNormal`. It's not good practice, though it might be OK in your situation. Any other code in the same process (maybe added later) would not expect a thread pool thread to have a non-standard priority. I would say a better alternative would be to set the process priority (`System.Diagnostics.Process.GetCurrentProcess().PriorityClass=System.Diagnostics.ProcessPriorityClass.BelowNormal`) which would affect all threads. – Olly Sep 05 '12 at 18:35
0

I would use ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(MyDeleteMethod), itemsTobeDeleted)

In MyDeleteMethod, you may consider breaking the deletion process in different bulks. In other words, you can specify a max number of rows to be deleted every time you are running the delete method.

If you are worried about number of available connections in the db pool, you can improve the performance by defining a static object (with a timer) and add the itemsTobeDeleted to that list(you need to use lock for sync access). Whenever timer Elapsed event fires, you can perform the bulk delete ( For instance you have 5,000 record and you are going to delete them 500 by 500 ).

Houman
  • 1,381
  • 1
  • 8
  • 10
  • Why using the UnsafeQueueUserWorkItem method rather than simply the QueueUserWorkItem? – Sharone Shani Sep 06 '12 at 11:51
  • Good question, difference is in verification of Security Privileges. Unsafe version doesn't care about privileges of calling code and runs everything in its own privileges scope which slightly improve the performance. – Houman Sep 06 '12 at 20:58