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,370 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 74

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

19 Experts available now in Live!

Get 1:1 Help Now