Archive for category C# Programming

.NET StringBuilder — Fast, but not as fast as you think!

I recently ran into a situation where I was tasked to profile some .NET code and do some optimizations anywhere hot spots popped up. I was amazed to find out that one of the BIGGEST offenders in our code block was a simple call to StringBuilder.Append(char). I had to take a step back and scratch my head and wonder if my profiler was confused.

I re-ran some tests using the StopWatch class to hard code some metrics into the application and they also confirmed the findings. What’s up? How could a class that everyone says you can use to your hearts content when it came to string concatenation was failing me?

Turns out, it was a mix of misuse and a common misconception about the StringBuilder Class.

Read the rest of this entry »

, , , , , , , , ,

No Comments

Example Huffman Compression Routine in C#

This last week I decided to sit down and hash out a simple Huffman compression routine using C#. I’d never created a compression routine before from scratch (my past implementations were static for the sake of time savings), so I fleshed one out. I know that many examples exist elsewhere on the net…. but they all seemed overly complicated and up their own ass :P

I had a couple goals in mind while creating my routine:

1. KEEP IT SIMPLE — A lot of routines out there WORK, but their code is too overly complicated for their own good. This over complication leads to slowness which brings me to my next goal ;) It should be a simple class that accepts input data, with simple public accessors that are easy to understand even for the novice developer (sorry folks, no asynchronous delegates). :P

2. MAKE IT FAST — When dealing with large amounts of data in C#, especially when running it through an algorithm, it’s all too easy to use all the handy built in virtual methods or using other build in tools which make coding easier with speed being the sacrifice. Die hard C++ developers will point to these routines as C#’s downfall as a legitimate language when it comes to data intensive tasks.

Read the rest of this entry »

, , , , ,

5 Comments

A new version of WWWinamp is coming… I SWEAR!

Hello Everyone!

I’m currently working on the latest version of WWWinamp after taking another hiatus from the project in order to clear my head and work on some other fun things such as the previously mentioned XNA projects :)

The next version of WWWinamp will be targeted for the .NET 3.5 Framework and you will need to have it installed on your machine in order to run WWWinamp. Most of the changes in this version are behind the scenes and will probably be transparent to you, the user. BUT, some visible changes in the upcoming version of WWWinamp 4.2 will be:

  • Automatic Version Checking for new releases of WWWinamp
  • Ability to enable or disable Automatic Version Checking
  • Ability to enable or disable Windows Vista warning message
  • Improved performance under load

I know, nothing earth shattering on the horizon. “Why is this?” you might as. Well, the honest reason is that I haven’t received any bug reports with issues that weren’t related to a misconfiguration or user error. This is a good thing! It means that WWWinamp is stable and there aren’t any horrible bugs that continue to nag users. The downside to this is that because WWWinamp is pretty feature rich as is, I’m running low on ideas that could be implemented.

An idea I’ve tossed about and am thinking about trying out is creating a WWWinamp .NET assembly for user within your own Windows or ASP.NET applications developed using the .NET framework. The assembly would handle the media library and searching of the library so you wouldn’t have to code your own database and file searching routines, as well as handling the unmanaged code required to interface with Winamp (if it’s running locally on the WWWinamp machine) or communicating via WCF to a remote instance of WWWinamp running the WCF daemon.

I think that between the WWWinamp Server and my proposed WWWinamp Assembly, that’d give both advanced and novice users full control over their setup. :)

As I mentioned in a previous post, I’ve moved WWWinamp into Visual Studio 2008 with Team Foundation Server. Within the latest version of Visual Studio 2008 there is a set of code analysis tools which calculate such items as Code Maintainability and Cyclomatic Complexity. I ran it on WWWinamp and it scored the following:

Maintainability Index: 83 (out of 100)

Cyclomatic Complexity: 636

Depth of Inheritance: 7

Class Coupling: 151

Lines of Code: 1,829 (!!)

After over a year, I think WWWinamp has gotten better and continues to improve with every version!

Big thanks to everyone for their support and keep your eyes peeled here for the latest version of WWWinamp!

2 Comments

2D WndrPong! using the Microsoft XNA Game Studio v2.0!

I decided to take some time this weekend to sit down and learn what I could about the latest release of Microsoft’s XNA Game Studio. I started out with a book I purchased called Microsoft XNA Game Studio Creators Guide, which turned out to be a terrible book. Most of the examples in this book assume that you’re starting with a project the book provides on a Website, which already has hundreds of lines of code, custom shaders and everything built in… without even explaining how the code is working in the background.

After fumbling around with that for an hour or so an only succeeding in creating a small square on the screen, I headed over to Microsoft.com to see if any MSDN articles might exist to help me along in my ‘ground up’ learning of XNA. I was pleasantly surprised when I found a great article titled “Your First Game: Microsoft XNA Game Studio in 2D“. This was EXACTLY what I was looking for as it starts from the ground up, assuming the reader has never done game programming before, let alone 3D game programming. :)

The example provided my Microsoft in this article is a simple 2D Texture of a cat that bounces around the window. I was so pleased with the ease of coding this, I thought to myself, “Heck, how hard could it be to recreate Pong?” :) So I set upon my task.

Several hours and many Coca-Cola bottles later I had not only my first XNA game running in Windows, but after purchasing the XNA Creators Club annual subscription from the XBox Live! marketplace for $99, I had it running on my XBox 360 as well :) I decided to take a little extra time and add a debug information screen as well as a small welcome screen :)

Controls (PC):

Up/Down for Player 1 Paddle (Left): Q/A

Up/Down for Player 2 Paddle (Right): Up Arrow/Down Arrow

Debug Information: F1

Controls (XBox 360):

Up/Down for Player 1 Paddle (Left): Left Thumbstick on Player 1 Remote

Up/Down for Player 2 Paddle (Right): Left Thumbstick on Player 2 Remote

I did run into a couple of ‘gotchas!’ while working with Game Studio. The major one I had trouble with was when you’re developing for the XBox 360 you have to account for overscan on the Television and have your game render within the ‘Safe Area‘. I noticed that when I was playing my Pong! game on my LCD TV at 720p, the edges of the game were cut off and it looked like it was stretched past the borders of my TV. After asking the fine folks in #XNA on IRC about this issue, they were able to help me out. Now I have my Pong! game account for this by setting up an XBox 360 macro which pads the edges of the play area by 50 pixels.

I’m including the Source Code for my version of Pong! which I’ve titled, “2D WndrPong!”. You can work in XNA Game Studio for free using Visual C# 2005 Express Edition along with XNA Game Studio 2.0 :) To deploy your games to an XBox 360, you must pair your XBox with your PC (using the “XNA Game Studio Device Center” tool) then you must purchase an XNA Creators Club Membership from the XBox 360 Marketplace. I believe the prices are $49.99 for 3 Months, $99.99 for a year. I opted for the entire year since I know it’s going to take me some time to learn ;)

Baby Steps :)

2D WndrPong! Source CodeDownload (36k)

, , , , ,

1 Comment

WWWinamp is being converted to .NET Framework v3.5!

Yep, it's true!

I've spent the last week or so playing around with the .NET Framework v3.5 and I really like what I see. Microsoft has also put a lot of work into Team Foundation Server 2008 as well. I've setup a Virtual Machine using Virtual PC here running Team Foundation Server 2008 on Windows 2003 Server R2 and was able to get WWWinamp imported + converted without issue!

WWWinamp in Team Foundation Server 2008
You need to enable javascript to view the content.
Power Thumbnail Powered By CodeXpress.CN

One thing I have noticed is that TFS does take up quite a bit of resources. I had to kick my VM up to 2GB of RAM in order for it to handle and build WWWinamp without hitting the swap file. Something to keep in mind moving forward I suppose. I'd imagine a large installation of TFS would require something along the lines of a Quad Core CPU with at least 4GB RAM.

There are several features in .NET 3.5 that I plan on implementing in WWWinamp right off the bat:

  • Object Initializers - This will really help clean up the already massive code base. It'll also speed things up a bit :)
  • Embedded Manifest - A feature I'm surprised they didn't implement with the release of .NET 3.0. Now the .manifest file will be embedded into WWWinamp, so you won't have the extra file handing around if you run Windows Vista :)
  • Extension Methods - Again, this will help clean up the code base and make things faster. I imagine it'll take up a little more memory, but these days RAM is abundant :)

Now, I see LinQ and I'm trying to figure out it's role in everything. I think it'll make DB communication easier but I'm not a fan of the anonymous data types and implicit local variables. I think it could lead to some sloppy coding methodology if not kept in check.

I think it's handy that you can now run a SQL like statement on a generic, but I'm wondering what sets this apart from the FindAll method that already exists in System.Collection.Generics. It does make it easier though to search through a Generic Collection for a developer who doesn't have a solid grasp on delegates.

So, all that said, things are moving along. I'll keep you all updated on the progress and please, send in those feature requests! :)

, , , , ,

1 Comment

WWWinamp Feedback Requested

I started tapping away at the WWWinamp source the other night and was curious if there are any features you guys, the users, would like to see added? I've been trying to think up new ideas but I'm not sure if they'd even be helpful.

So basically, I'm asking help for what to put in the next version. If you could leave a comment with any of the following, that'd REALLY help out! :)

  • What feature would you like to see added to WWWinamp?
  • Is there any nagging bug or weird behavior you would like fixed?

I'm currently fiddling around with Visual Studio 2008 right now and am looking into the benefits C# 3.5 offers over 2.0, which the current release of WWWinamp is based upon.

Thanks for your feedback and support everyone! I look forward to your responses! :)

, , ,

3 Comments

Discogs.com API Assembly for .NET Applications v1.0 Build 2876

Hello Everyone!

I'm very pleased to bring you the latest build of the Discogs.com API Assembly :)

This version bring more error handling reguarding the Cover Art routines as well as the ability to now specify your own Discogs.com API key :) This is not a REQUIRED field, and if you do not specify an API Key value it will default to the API key I've already registered for the Assembly. I figured this would be the easiest way for people to use it.

Some have expressed their concern on the daily usage limits that are imposed. To clarify these limits, as stated on the API information page, the daily limits are 5,000 requests per day, PER IP. This means each person using the API Key can use it up to 5,000 times per day. So no worries on gobbling up all available tries! :)

If you still want to register for your own API Key, you can do so here!

So, to recap the changes in this version:

  • Better Error Handling in GetCoverArt() method
    • Will return a blank 1x1 Bitmap if an Exception is thrown.
  • Better Error Handling in SaveCoverArt() method
    • Will return FALSE if an Exception is thrown while trying to save the file
  • Able to now specify your own API Key (API_KEY). If you do not specify one, it will use a default API Key (which works just fine!)

Cheers!

Discogs.com API Assembly for .NET Applications v1.0 Build 2876 - Download (9k)

14 Comments

Discogs.com API Assembly for .NET Applications v1.0 Build 2871

Greetings Everyone!

I've been working on the Discogs.com API Assembly for .NET Applications now for a couple days and have been able to make some progress. It's now a bit more stable as well a a tiny bit easier to use.

I took some time and added a DEBUG class. This class allows you to find out what's happening within the Discogs.com Assembly if you start to have issues! :) This new class has two properties:

  • Verbose (bool) - If set to TRUE, Verbose logging will be enabled allowing you to get more precise detail on what is going on within the Assembly. Otherwise, only exceptions will be logged.
  • Log (string) - This is a string containing the current debug log.

Also, it has one Method:

  • LogEvent (string sEvent) - Logs the value passed in to the debug log. This way you can use the same debug log from your own applications :) Should help make things a little easier.

New in this version as well is better error handling in the event of 404's or an Artist/Release isn't found. Before if you requested something that didn't exist, the Assembly kinda crapped out while trying to deserialize the (non-existent) XML :) This has all been fixed.

I'm also including a small example program (with source code) on how to use the Discogs.com API Assembly. I've coded the example in C#, so sorry to all those VB.NET developers out there! :) If one of you guys would like to translate it to VB.NET, I'd be more than happy to post it here as well.

If you do not already have Microsoft Visual Studio installed, no worries! Microsoft provides a free version for C# development called Visual C# Express and you can get it here over at Microsoft.com. :)

You will need to update the Reference to the Discogs.com API Assembly. It currently points to where I had it setup on my local machine. :)
Any and all feedback is appreciated!

Cheers!

Discogs.com API Assembly for .NET Applications v1.0 Build 2871 - Download (9k)

Discogs.com API Assembly Example Application (with Source) - Download (10k)

, , , , , ,

1 Comment

Discogs.com API Assembly for .NET Applications v1.0 Build 2867

Hey Everyone!

I've been putting some time into my Discogs API Assembly hoping to implement all the functions supported by the API. This build should give access to all available API functions:

  • Get Release Information - SearchRelease(string sRelease)
  • Get Artist Information - SearchArtist(string sArtistName)
  • Get Label Information - SearchLabel(string sLabel)
  • General Search - Search(string sSearchTerm)

I've changed the method signatures for the Cover Art downloading routines. I've now split this into two new methods:

  • GetCoverArt - This method returns the cover art image that's downloaded from Discogs.com as a bitmap
  • SaveCoverArt - This method will save the cover art image that's downloaded from Discogocs.com to the specified file and folder

This way it will be easier to anyone to display the cover art image easily within their application (perhaps a preview window?) without actually having to save anything to the disk.

I don't suspect the method signatures will be changed much from this version, as these are the only functions provided in the API currently. I still need to work on the error handling within the Assembly itself, such as 404 error handling and also any errors in deserialization. I might make a toggle which will enable verbose debug output from the assembly to a file you specify. This would help developers work with me in fixing any issues that might come up, as well as helping them trouble shoot their own application.

I'm still working on my Simple MP3 Renaming application written in C# using this Assembly. It's hard to spit my time between the two, but it's been my test harness for this. I'm going to be releasing the source code under the GPL v2 license.

Cheers!

Discogs.com API Assembly for .NET Applications v1.0 Build 2867 - Download (8.9k)

No Comments

Discogs.com API Assembly for .NET Applications v1.0 Build 2863

Well, here it is! The Discogs.com API Interface Assembly for .NET Applications.

After fiddling around with the Discogs.com website I decided to take a look at their developers API. It was actually pretty slick, as it lets you look up an Artist and retrieve all of their information including album information on every album they've been associated with (including soundtracks). I thought this information might be handy for other people so I wrote a quick Assembly in C# that lets people run queries against the Discogs API easily!

This Assembly provides only three methods currently:

  • SearchArtist (string sArtistName)
    • This method will search the Discogs Library based on the Artist Name specified and return a complex type containing the API response.
  • SearchRelease (string sReleaseName)
    • This method will search the Discogs Library based on the Release Name specified and return a complex type containing the API response.
  • DownloadCoverArt(string sFileName, string sURL)
    • This method allows you to download the cover art from the Discogs Library using the Cover Art URL contained in the Release API response.

I've also included a Version property so you can bind your own application to a specific version of the Assembly, in case there's any version incompatibility moving forward.

The assembly currently already has a Discogs.com API key hard coded in. So no need to sign up for your own API key.

Also, it seems that the API is constantly changing so if for whatever reason one day you stop receiving information from the API, please let me know. Any changes to their XML response might cause the assembly stop responding with information.

My hopes is that people will put this Assembly to good use in their own Media applications. My plan is to release an Open Source MP3 renaming application written in C#. Keep your eyes peeled for that in the coming weeks.

Share and enjoy!

Discogs.com API Assembly for .NET Applications v1.0 Build 2863 - Download (6.8k)

2 Comments