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,420 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 60
Finding the IIS version 5 22
linq, c# 8 24
PowerShell: ForEach ($y in $x.licenses | where {....} 3 23
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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