Life is like Code, you never know what you will create next

Multi-Threading with C# and a Website

23. November 2011 00:40 by Scott in Development, C#  //  Tags: , , ,   //   Comments (6)

Let me tell you my experience.

I have a website at that gets hit well over 1 Million times a day.  Thats 11 hits every Second.  Utopiapimp works on being a Live Stock Ticker for a online game called Utopia.  My users need live feeds 24/7 of whats happening in the game and thats what my application does for it.  Live feeds 24/7 for all users.  Its extremely dynamic as well.  Always changing, always updating.

My server that hosts my website is a VPS located with the company Ultima Hosts.  I have a middle tiered plan that costs a bit to host.  The advertising on Pimp pays for the hosting fees, but it doesn’t cover any more than that.  I have 4 GBs of Ram and 1 CPU.  I store so much in the caching system, that I take and use up at times almost all 4 GBs of Ram.  The problem mainly is the CPU.  At times the CPU hits 80% used and other times it coasts around 15% used.  So I was hoping to bring in some multi threading into Pimp to make the inserts into the DB much faster and a much faster response time for the users.

I started with just one thread, the new type of tasking in C# introduced in .Net 4.0.

Task.Factory.StartNew(() => DoSomeWork());

The Problem with this I think, is I only have one CPU.  When I implemented this method, I sadly and immediately saw a block.  The application started to hang.  I spent the next few weeks to try and stop the hang.  Yes, sadly, I introduced a bug so bad, I immediately started getting user connections loss.  I introduced a ton of code when I introduced this bug, so I was completely lost on the topic.  I thought well, it had to be something else.  Not until I wrapped the task around my error handling code to insert errors into the DB, did I realize what was going on. So I tried threading instead.

System.Threading.Thread newThread = new System.Threading.Thread(anObject.AMethod);

This didn’t work either.  I then realized, well maybe threads just don’t work on .Net Web apps.  I then thought, well maybe its just my 1 CPU.  Well, I haven’t yet upgraded to the second CPU, to test this out.

But believe me, when I get the chance, I will.


How To Count Lines of Code in Visual Studio

22. November 2011 02:00 by Scott in Visual Studio, Tricks  //  Tags: , , ,   //   Comments (0)

Measuring programming progress by lines of code is like measuring aircraft building progress by weight. - Bill Gates


If your like me, then you want to know at times how many lines of code you have written over the years.  I have this one web site that I thought would be roughly 200,000 lines of code.  Its over at  I questioned today how many lines of code I have when I was in discussion with one of my coworkers.  He didn’t think that my website offered that much and well, I had to prove him wrong.  So in figuring out how many lines of code I wrote, I had two different ways.  Here are the ways.

When using Visual Studio Team System:

Analyze -> Calculate Code Metrics has the ability to count projects of code, but not actually web forms applications.  The problem with this, is that I actually have a website and not an actual C# project, So I used the second method.

Visual Studio Trick:

This method will will match every line that are not comments (//), a compiler directive (starts with #), a single opening or closing brace, or blank lines. So these are true lines of code.

  • Select Edit -> Find & Replace -> Find in files… or just press CTRL+SHIFT+F
  • Check Use and select Regular expressions.
  • Top Left Drop down using the Find in Files Selection
  • Type the following as the text to find:

for C#


for VB.NET (thanks Steve for the info!)



  • If you select Current project or Entire solution, you also need to specify the file types that will be included in the search.

Proceed with the “search” and at the bottom of the Find results window you will see the total line count.

Objects in this class cannot be updated outside an edit session ESRI

15. February 2011 13:33 by Scott in   //  Tags: , , ,   //   Comments (0)

I received this error while working on some ArcObjects Code.

Objects in this class cannot be updated outside an edit session.

It had to do with the relationships that I established inside the File Geodatabase I was saving to.

So I had to add a few lines and open up an EDIT SESSION on the workspace.  I guess if you don’t have any relationships, you don’t need an edit session, but when you start using relationships you have to start opening up an edit session.

So here is my code.  You will notice the 3 lines that open the IWorkspaceEdit and the last two lines that stop editing.

//First thing is to create a generic fGDB workspace factory class
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
//Create a workspace and point it to the fGDB file
IWorkspace workspace = workspaceFactory.OpenFromFile(DATABASE_PATH_WITH_ATTACHMENTS, 0) as IWorkspace;

IWorkspaceEdit edit = (IWorkspaceEdit)workspace;

//Add some EDIT code here to the DB...

//Remove references to the fGDB
workspaceFactory = null;
workspace = null;
datasets = null;

How to Create a Threaded Comment System with ASP.NET

4. February 2009 03:49 by Scott in   //  Tags: , , , , ,   //   Comments (1)

A few days ago I was designing a threaded comment system for which allows users to reply back to other users directly instead of a flat comment system like you see at most other places.  I made my argument for threaded comments over here and why they should be used.  I didn't know where to start or how to write the code.  I had a pretty good idea on database design which was decently simple.  Each comment shall have an owner comment.  The treeview is the best example of what the data in the database should look like.  As for how to display this out to code is another problem.  I ended up searching on the internet and found a man who created hierarchy class which is exactly what I needed.  The Hierarchy class selects the information from the database and organizes it into a cached treeview.  I ended up talking to him and he had explained to me that he was refactoring the class which will allow it to run faster.  As for how I implemented the code to write out HTML, I ran through the hierarchy class recursively and then created html for each node found.  The class was quite impressive and I included some sample code below.  I didn't want something like this to be wasted and hope everyone can put it to good use.

Photo by jurvetson

I also wanted to give a shout out to Stefan Cruysberghs and say thank you for creating this class and posting it up on the net.  Now to keep with the current standard, I will post up my execution of his code.

ThreadedComments.cs (5.98 kb)

If you liked this post, please be sure to subscribe to my RSS Feed.

Building a Linq Query gives me a question.

3. April 2008 23:12 by scott in   //  Tags: , , ,   //   Comments (17)

So here I am building a new site that will estimate around 500,000 page views and 50,000 database transactions DAILY.  I am currently working on a simple query that selects ONE row[column] from a database and returns the value to the rest of the methods.  I have a simple thought or question I would like answered and thought this would be a place to bring it up.

Is the foreach loop required even if I know I will receive only 1 record back from the database?

That's it and to give you an example:

        CS_Code.DataContext db = new CS_Code.DataContext(SQLStatementsCS.ConnectionStringID());
        var query = from RN in db.Province
                    where RN.Name == Name
                    select RN.uid;

        int value;
        foreach (var uid in query)
        { value = uid; }

I think it is kind of pointless to create a foreach loop and use 3 lines of code to just get an ID out of the database. So let me know in the comments section if this is the only answer and I will point out the answer and give you a link back to a site of your choice.

Thanks guys and dolls.

kick it on