Solved

Is creating a timer/separate thread within an ASP .Net MVC Web application a bad thing to do?

Posted on 2014-04-11
6
1,432 Views
Last Modified: 2014-04-17
Hi:

We have an ASP .Net MVC web application.

In order to do some processing on set intervals, I'm looking at the possibility
of creating a timer or a thread within the app (server side)  to do the clean up.

Is this a good design practice (Am I breaking some kind of rules here)?
What are the possible short comings of such an approach?

Thanks,
JohnB
0
Comment
Question by:jxbma
  • 3
  • 2
6 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39995322
Its fine to create a separate worker thread. There are two things to keep in mind (in my opinion). The first is that it the website isn't accessed for a period of time, the application shuts down, shutting down your worker thread. The second is to make sure it doesn't slow down the start up of the site itself, or slow down the site while its running. Other than that go for it.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39995351
@dale_burrel

There are two things to keep in mind (in my opinion). The first is that it the website isn't accessed for a period of time, the application shuts down, shutting down your worker thread.
Is that true if the thread has its IsBackground property set to false?

@jxbma

So long as you aren't expecting the separate thread to return data to the user, then it should be fine--so long as you don't consume all of your resources, like any thread usage should be concerned with. If you do need to return data to the user, then you would need a different (or perhaps modified) approach.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39995360
@kaufmed I believe so, quoting your link "Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete."
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:jxbma
ID: 39999146
What about when this MVC application gets scaled out across a server farm?
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39999228
The only issues that I am aware of when you start talking about farms is regarding session handling. My experience with farms is limited, though.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 250 total points
ID: 40003255
The thread you create will only run within one of the processes on one of the servers handling your site - unless you specifically write your code to run it on all of them.

In fact you will need to be very specific in how to write your code, because you will probably either only want one instance of it running or an instance in every process. Either way will add some complexity, because normally one would use a mutex to ensure you only get a single instance, but that won't work across multiplse servers.

And regardless of how many servers are handling requests to your site, if they all shut down due to inactivity your process stops. If you only have one instance and that process shuts down your process stops.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC - Views 3 38
asp.net mvc 2 32
VB.NET - Refactor Class per SOLID principles 2 33
IDE for Python 5 62
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question