§ Coding
Coding samples, tips, tricks and other stuff...
Wednesday, November 17, 2010
A pitch to move to a virtual machine based development environment
I recently moved to a VM (Virtual Machine) based development work environment and wanted to share my experience with others and provide some tips for getting started.
I’m sure you’re probably asking yourself, what is a VM based development environment? Well simply put the idea is to do use VM software such as; VMWare Workstation, Oracle VirtualBox, Microsoft Virtual PC to create one or more virtual computers in which you install and use your development tools.
There are many reasons you might want to move to a VM based development environment.
Experimentation
My primary development environment is setup using Windows 7 Ultimate (x64) with Visual Studio 2010 Pro and SQL Server 2008 R2 (x64). What if I decide I want to try doing some development using the free and open source MySQL database? Well I guess I could install all that on my machine and hope it doesn’t have any compatibility issues or screws up my computer. However with VMs I can easily clone my development environment and install MySQL safely in this copy. I can verify that it causes no issues and develop within that new system for whatever projects I want to use MySQL for. If I get bored or decide I don’t want to use MySQL it won’t hurt one bit to just delete the entire VM.
Portability
Do you use many different computers? I know I sure do! I have my work PC, my home desktop and my laptop. If I’m using VMs to host my development environment I can keep the VM on an external USB drive and as long as I can install the VM software on the computer I’m at I can start up and use my development environment any ware, any time.
Testing
Besides just installing your development tools in a VM you can setup separate environments to test your software in different platforms. Want to see how your app works on Windows 2000, XP, Vista, Linux? Not a problem when you can setup multiple installs inside separate VMs. Plus most VM software allows you to setup what are called snapshots. Snapshots allow you to save the state of a VM and rollback changes at will. I won’t go into details about all the power that provides but it’s something you should look into because it is a great feature!
Security & Privacy
Are you a consultant that works for many different clients? I’m sure at some point you will end up working with a client that goes overboard with security. You know the type, they require you use a special VPN client and it won’t let you log into the VPN if you don’t have a specific required anti-virus software. The power of virtual machines allows you to make a complete copy of your development VM and then you can install whatever required software you need for that client.
Plus as an added benefit you can tell your clients that the code you work on will be secure and private in a dedicated development environment that no one but you has access to and will not be shared with any of your other clients. Most software a company has is private and very valuable. I’m willing to bet letting a client know that the all their code, documents and files will be kept in secure and private environment will go a long way to showing you are very professional.
Freedom
This one I admit probably isn’t exactly the most upstanding of reasons but I’m not going to lie and say it’s not a valid one I think about.
Are you stuck at work using Windows XP and Visual Studio 2005? Well there’s a good chance your work PC is probably also a horribly slow hunk of junk too. If however you have some decent hardware you could setup a VM at work so you can run your favorite OS and dev tools. No one has to know, we’ll keep it just between you and me. I promise!
Of course, unless you are a one man dev shop, you need to be good and make sure the work you do is still compatible with other developers using old tools. Some might call it job security but I call it being an inconsiderate ass if you develop software that can only be recreated with your toolset.
Hopefully this short post has brought some new ideas about Virtual Machines and their benefits for developers. Take a look at one of the free VM offerings, install and start playing around.
Best of luck!
Monday, May 17, 2010
C# Mutex for running single instance of a program
Here is some sample code that can be used in a c# application to make sure that only one instance of the program can run at a time. In order to do this you will use a Mutex (Mutual Exclusion) which is a type of system wide lock. In laymens terms a Mutex is like a claim to ownership of a idea. You’re telling the world that no one else can have that idea as long as you are holding claim to it. As so as you don’t need it you can let go of the ownership and then someone else can claim ownership.
Sample Code:
class OneAtATimePlease {
// Use a name unique to the application (eg include your company URL)
static Mutex mutex = new Mutex (false, "oreilly.com OneAtATimeDemo");
static void Main() {
// Wait 5 seconds if contended – in case another instance
// of the program is in the process of shutting down.
if (!mutex.WaitOne (TimeSpan.FromSeconds (5), false)) {
Console.WriteLine ("Another instance of the app is running. Bye!");
return;
}
try {
Console.WriteLine ("Running - press Enter to exit");
Console.ReadLine();
}
finally {
mutex.ReleaseMutex();
}
}
}
A good feature of Mutex is that if the application terminates without ReleaseMutex first being called, the CLR will release the Mutex automatically.
As you can see it’s pretty simple code. You create a Mutex object. However, until you call the WaitOne function you do not actually make your claim to ownership of the mutex. So remember to do that!
* Disclaimer: This code and content are taken from multithreading in C# by Joseph Albahari. I do not claim any ownership or rights to this content. I am merely copying for my own future reference and to share the knowledge.
Page 1 of 1 pages