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,406 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programmatically storing deadlock graphs with SQL Server 6 40
Cant save 3D 4 19
No Data for DropDown List 2 22
VB.NET 2008 - SQL Timeout 9 22
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now