Tuesday, January 19, 2010

Code: Serializing and Deserializing Objects (Converting Objects to a String and Back)

Object serialization in .Net is pretty easy, once you have the right code.  It took me a few minutes to figure out the right code so I thought I’d post it here.  The code below is a working C# console app.

Code Snippet
  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  4.  
  5. namespace TestApp
  6. {
  7.     public class Program
  8.     {
  9.         static string serializedObject;
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             Serialize();
  14.             Deserialize();
  15.         }
  16.  
  17.         static void Serialize()
  18.         {
  19.             Person p = new Person() { Age = 20, Name = "Bob" };
  20.             XmlSerializer serializer = new XmlSerializer(p.GetType());
  21.             using (StringWriter sw = new StringWriter())
  22.             {
  23.                 serializer.Serialize(sw, p);
  24.                 serializedObject = sw.GetStringBuilder().ToString();
  25.             }
  26.         }
  27.  
  28.         static void Deserialize()
  29.         {
  30.             XmlSerializer serializer = new XmlSerializer(typeof(Person));
  31.             Person p = (Person)serializer.Deserialize(new StringReader(serializedObject));
  32.             Console.WriteLine("Name:" + p.Name + " Age:" + p.Age);
  33.             Console.ReadLine();
  34.         }
  35.  
  36.         public class Person
  37.         {
  38.             public int Age { get; set; }
  39.             public string Name { get; set; }
  40.         }
  41.     }
  42. }

Friday, January 15, 2010

Code: Multiple Startup Projects in Visual Studio

Visual Studio has the capability to have multiple projects listed as startup projects.  This will allow you to launch and attach debugging to multiple applications at once.  This is very handy when debugging the encoding system locally.
To do this right click on the Solution icon in Visual Studio and select Properties.
Check the "Multiple Startup Projects" radio button
in the list of projects below the radio button change the Action to "Start" for the projects you want to launch.

image

Thursday, January 14, 2010

Code: Launch a Console Application in a New Window

I ran into a tricky situation today, How do I use a batch file to launch other batch files or console applications in a new console window?  The key to this is the START keyword.  So if you open up a new command prompt window and enter

test.bat

You will effectively transfer control of your current window to the test.bat file.  However, if you change your command to

start test.bat

The batch file will be launched in a new window allowing your first window to continue operation to perform other commands or launch more programs.

Wednesday, January 6, 2010

Code: Build an Adaptive Bitrate SMIL File Using XmlDocument

Code Snippet
  1. /// <summary>
  2. /// Builds a smil file based on .mp4 files found in a given directory
  3. /// Assumes files are named XXX_bitrate.mp4 (movie_300.mp4)
  4. /// </summary>
  5. public static XmlDocument BuildSmilFile(DirectoryInfo sourceDirectory)
  6. {
  7.     List<FileInfo> files = new List<FileInfo>();
  8.     files.AddRange(sourceDirectory.GetFiles("*.mp4"));
  9.  
  10.     //biggest file (highest bitrate) first.
  11.     files.Sort(delegate(FileInfo p1, FileInfo p2)
  12.     {
  13.         return p2.Length.CompareTo(p1.Length);
  14.     });
  15.  
  16.     XmlDocument result = new XmlDocument();
  17.  
  18.     //create element nodes
  19.     XmlNode smilNode = result.CreateElement("smil");
  20.     XmlNode headNode = result.CreateElement("head");
  21.     XmlNode bodyNode = result.CreateElement("body");
  22.     XmlNode switchNode = result.CreateElement("switch");
  23.  
  24.     //add video nodes for each file
  25.     foreach (FileInfo mp4File in files)
  26.     {
  27.         XmlNode videoNode = result.CreateElement("video");
  28.  
  29.         //Create and add attributes
  30.         XmlAttribute srcAttr = result.CreateAttribute("src");
  31.         srcAttr.Value = mp4File.Name;
  32.         XmlAttribute bitrateAttr = result.CreateAttribute("system-bitrate");
  33.         bitrateAttr.Value = Path.GetFileNameWithoutExtension(mp4File.Name).Substring(mp4File.Name.LastIndexOf("_") + 1) + "000";
  34.         videoNode.Attributes.Append(srcAttr);
  35.         videoNode.Attributes.Append(bitrateAttr);
  36.  
  37.         //add video node to switch
  38.         switchNode.AppendChild(videoNode);
  39.     }
  40.  
  41.     //append nodes to parent elements
  42.     bodyNode.AppendChild(switchNode);
  43.     smilNode.AppendChild(headNode);
  44.     smilNode.AppendChild(bodyNode);
  45.     result.AppendChild(smilNode);
  46.  
  47.     return result;
  48. }

Monday, January 4, 2010

Electronics: Plasma vs. LCD

It seems as if the nation has fallen out of love with plasma televisions, and I can’t figure out why.  Generally speaking plasma’s have better color, higher refresh rates, and deeper blacks than LCD TVs.  Typically the counter argument is that plasma’s use more electricity and their screens are more reflective, which can be a problem in rooms with windows.  Recently I purchased a 42” 720p Panasonic TC-P42X1 plasma to replace my 3 year old 720p 42” Sony Wega LCD projection TV. 

The picture quality of the plasma is outstanding.  One of my biggest complaints with my old Sony was the black levels. This was the main reason I wanted to get a plasma over an LCD, and the Panasonic does not disappoint.  Furthermore it has a 600hz refresh rate so I never notice any ghosting in action movies.  I bought this TV for under $600.  Comparable LCD’s will run you $600-$1000, but at $600 for an LCD you’ll be getting an off-brand with mediocre picture quality.  My friend recently spent $1500 on a 50” 1080p 120hz LCD TV, and I’d take mine over his any day.  Motion images on his 120hz set are so unrealistic that it’s distracting.  Also the color was overly bright, but that was probably more a result of his choice of settings.

The power consumption of TC-P42X1 is also very good.  According to tests done by televisioninfo.com it used between 80 and 200 watts depending on how much white was on the screen at once.  That’s probably the same amount of energy you are using to light your room if you’re not using CFL bulbs.  A similar sized LCD set consumed 137 watts, so I’d actually call the power usage a wash.

Although Panasonic has done work to create anti-glare screens on their plasma’s, I definitely can’t watch the TV with the windows uncovered.  However we do have window blinds in our living room so its a non-issue for me.

Also this television is not 1080p, but I personally think 1080p is overrated.  Right now the only content in 1080p is blu-ray disks, which are expensive, and I don’t have a blu-ray player anyway.  I’ve never watched 720p content and wished for a higher resolution.  I’ll take a 720p TV with great colors and blacks over a poor 1080p TV any day.

So the rest of the world can enjoy spending extra money on LCD and LED sets, I just hope cheap, great plasma’s are still around the next time I need to purchase a TV.

Monday, November 23, 2009

Tech: Uninstall Windows Live Messenger on Windows 7

Windows Live Messenger isn’t visible in the Programs and Features list in Windows 7, however Windows Live Essentials is, and clicking it will open a menu that will allow you to uninstall Live Messenger.  I really dislike the interface on Messenger, but I put up with it so I could use the voice and video chat services.  However messenger recently started starting itself and opening up chat windows randomly so it is now banned from my system.  I’ll use Pidgin instead, or try to convince my coworkers to use Google Talk.

Friday, November 6, 2009

Code: Windows Server 2008 R2 and Microsoft Message Queuing (MSMQ)

We’re nearing the end of a large development project in which we used MSMQ extensively.  All of our queues are private and transactional.  For the most part MSMQ works great and serves its purpose excellently.  Save one glaring issue: We have had serious issues trying to send messages to the queues from remote machines.  All of the machines are in the same domain (and location).  We’ve had the environment set up on 3 different machines (for various reasons).  The first two installs we seemed to be able to post to the queues, although it took many tries changing security permissions on the queues and changing connection string formats (TCP vs OS).  On this 3rd environment we have been totally unable to send messages to the queues from a remote machine.  The messages get eternally stuck on the sending machines outgoing queue.  In the end we believe it to be an issue with the security lockdown of Server 2008 R2.

To get around the issue we had to install MSMQ HTTP Support and send the messages over HTTP.  To do this IIS must be already installed.  Then go into Features and enable Message Queuing > Message Queing Services > HTTP Support.  This will install a MSMQ Virtual Directory/Application on the default web site.  After that we just needed to change the connection strings to below and now things work great.

Code Snippet
  1. FormatName:DIRECT=HTTP://192.168.230.86/MSMQ/Private$/transcodejobqueue