Tech in the 603, The Granite State Hacker

Locking Resources in C# for Read/Write Concurrency

In a previous project, I became a big fan of System.Threading.ReaderWriterLockSlim.  It was an excellent way to guard a resource against concurrency in a relatively flexible manner.  

C# has a lock(object) {} syntax for simple concurrency locks, but what if you have a resource that can sometimes be used concurrently, and other times, exclusively?

Enter System.Threading.ReaderWriterLockSlim.  This has a few handy methods on it for guarding code on a non-exclusive (Read) and exclusive (Write) mode, with an upgradeable lock, as well, so you don’t have to release a read lock in order to upgrade it.

This source works just as well in .NET as UWP.

I commented the code enough to try to make it so that someone familiar with ReaderWriterLockSlim and using(IDisposable){} would understand the rest, so without further ado…

https://gist.github.com/GraniteStateHacker/e608eecce2cb3dba0dbf4363b00e941f.js

Tech in the 603, The Granite State Hacker

Live Process Migration

For years now, I’ve been watching Microsoft Windows evolve.  From a bit of a distance I’ve been watching the bigger picture unfold, and a number of details have led me to speculate on a particular feature that I think could be the next big thing in technology….   Live process migration.  

This is not the first time I’ve mused about the possibility… [A big feature I’d love to see in Windows 11] it’s just that as I work with tools across the spectrum of Microsoft’s tool chest, I’ve realized there are a few pieces I hadn’t really connected before, but they’re definitely a part of it.

What is live process migration?  Folks who work with virtual machines on a regular basis are often familiar with a fancy feature / operation known as live virtual machine migration….  VMWare’s vSphere product refers to the capability as vMotion.  It’s the ability to re-target a virtual machine instance, while it’s running… to move it from one host to another.

In sci-fi pseudo psycho-babble meta physio-medical terms, this might be akin to transitioning a person’s consciousness from one body to another, while they’re awake…  kinda wild stuff.

As you can imagine, live VM migration is a heavy duty operation… the guest machine must stay in sync across two host computers during the transition in order to seamlessly operate. For the average user, it’s hard to imagine practical applications. 

That said, live process migration is no small feat either.  A lot of things have to be put in place in order for it to work… but the practical applications are much easier to spot. 

Imagine watching a movie on Netflix on your Xbox (or maybe even your Hololens), but it’s time to roll.   No problem, with a simple flick gesture, and without missing a beat, the running Netflix app transitions to your tablet (or your phone), and you’re off.   Then you get to your vehicle, and your vehicle has a smart technology based media system in it that your tablet hands off the process to.   It could work for any process, but live streaming media is an easy scenario.

From a technical perspective, there’s a bunch of things required to make this work, especially across whole different classes of hardware…  but these problems are rapidly being solved by the universal nature of Windows 10 and Azure.

Commonality required:

  • Global Identity (e.g. Windows Live)
  • Centralized Application Configuration
    • Windows 10 apps natively and seamlessly store configuration data in the cloud
  • Binary compatibility
    • Universal apps are one deployable package that runs on everything from embedded devices to large desktops and everything in between.
  • Inter-nodal process synchronization
    • Nothing exemplifies this better than the 1st class remote debugging operation  in Visual Studio.  You can run an app on a phone or device from your laptop, hit breakpoints, and manipulate runtime state (local variables) from the laptop and watch the device react in real time.
  • Handoff protocol
    • I’m sure it exists, but I don’t have a good word to describe this, but it’s probably based on something like SIP
  • Runtime device capability checking (the part that sparked this blog post).
Over the years, there have been a lot of “write once, run anywhere” coding schemes.  Most involve writing a program and having the compiler sort out what works on each type of hardware…. what you get is a different flavor of the program for different kinds of hardware.  In Windows 10, it’s different.  In Windows 10, the developer codes for different device capabilities, and the application checks for the required hardware at run time.  
While the UWP does an amazing job of abstracting away the details, it puts some burden on the hardware at runtime…  the app developer has to write code to check, anyway: hey, is there a hardware camera shutter button in this machine?  If yes, don’t put a soft camera shutter button on the screen, but now the app has to check the hardware every time it runs.
I struggled a bit trying to understand this latter point…  why would Microsoft want it to work that way?  Except for a few plug & play scenarios, it could be optimized away at application install time…  unless your process can move to a different host computer/phone/console/tablet/VR gear.
While I am (more recently) a Microsoft V/TSP working for BlueMetal, an Insight company, I have no inside information on this topic.  I’m just looking at what’s on the table right now.   We’re almost there already.  Yesterday, I showed my son how to save a document to OneDrive, and within moments, pick up his Windows 10 phone and start editing the same document on it.
In my mind, there’s little doubt that Microsoft has been working its way up to this since Windows Phone 7… the only question in my mind is how many of these tri-annual Windows 10 updates will it be before “App-V Motion”-style live process migration is a practical reality.
Tech in the 603, The Granite State Hacker

A big feature I’d love to see in Windows 11

With all the announcements coming out of //Build, I’m pretty jazzed about what’s coming in Windows 10.   That doesn’t stop me from wishing there were one or two other scenarios Microsoft would get to… and at this point, I’ll have to hope to see them in something after Windows 10.

“App-V-Motion” running apps, migrating them across devices. 

Enable an app running on the phone or tablet or laptop or desktop to seamlessly transition from device to device.

Imagine it’s getting late in the day…  you have a long running process on your desktop that you need to babysit.  Poor timing, sure, but it happens far too often.   Now, rather than being tethered to your desk, you can transition the process to a mobile device, and simply take it with you.   Perhaps it’ll take longer to complete on the mobile device, so when you get home, you hand it back off to bigger iron. 

or, my other favorite scenario…  you’re watching your favorite movie, but it’s time to roll…. so you hand off the movie player app to your phone, and keep watching while you’re on the go, without missing a beat.

With cloud configuration & storage, this scenario is getting more and more feasible, but given where I’m seeing Windows 10, now, this could potentially be a 10.1 or 10.2 feature.

Tech in the 603, The Granite State Hacker

Virtual Gambit

What a pain.

A few weeks ago I posted a bit about how Windows 8 is not killing the PC market.

I’ll now take that a step further and argue about the one thing in Windows 8 that is really making me regret not having new hardware…  an actual compelling reason to really think about a new PC, for me.

Around the time I wrote the original post, I was looking at taking advantage of Windows 8 Pro / Hyper-V on my home PC…  mostly because emulators for devices such as Surface and Windows Phone 8 make use of it.  I have Hyper-V on my work system, and it’s fantastic for a variety of reasons.

I’d all but forgotten that my home PC didn’t have a processor that supported VT-x technology, meaning no Virtual Machines… so when I went to add the Hyper-V role, I was somewhat disappointed to discover it was a no-go.  

“Maybe it’s time to upgrade something,” I thought.   I could get a new machine… but it’s pretty hard to justify the chunk of change based on this one feature.  (Never mind that it’s not so easy to liberate that kind of money from the budget.)  “I’ll order a new processor that supports Virtual Machines.”  VT-x.

So I dropped the $200, instead…. placed an order for a dated, but still more feature-rich chip.  I waited for the thing to arrive, and finally sat down, lifted the old CPU out, and dropped in the new one.

After the successful screw-top surgery, I pulled up the dialog to add Hyper-V only to discover that…  the new CPU doesn’t have the guts to run Hyper-V on Windows 8 either.

Seriously miffed, (because I know VM technology is older than Hyper-V, and I just burned $200 for 2-3 more FPS in WoW) I started looking into what the deal was.  Turns out Hyper-V on Windows 8 requires an additional virtualization technology in the processor called SLAT.  

None of the processors that fit my home PC support SLAT. 

If I want Hyper-V, (which is highly desirable for coders like myself who want to use Microsoft tools for the latest MS platforms) I *must* replace the vast majority of my PC’s components… but, really… economically speaking…. I need a new PC.

So it’s an interesting conundrum that I can’t help think was designed around boosting hardware sales….  Seriously…  I can do a lot with Virtual Box and VMware Player in terms of running VMs.  I could even install Windows Server 2012 and run Hyper-V from the server OS (without SLAT) on the very same hardware.  (Only Windows 8 Pro Hyper-V requires it.)

I can’t buy a new PC for less than $2k that will outperform my current system…  so, when it comes down to it, the ONLY reason I can think of to upgrade would be for Hyper-V… what a pain.

Here’s a comparison between my PC (Hyper-V no-workey) and a much more modern i7 (Hyper-V workey)…  note that the i7 is an Intel i7-720M, but it’s processor score (6.9) in the Windows Experience Index is less than the experience index of my older Yorkfield Core 2 Quad-Q9400 (7.2).   (both systems are hobbled by their disk platters).

Tech in the 603, The Granite State Hacker

Windows 8 Hyper-V Advanced Tips For ESATA

Here’s a couple tips for a perhaps not so unique situation when using Hyper-V hosted in Windows 8 Pro with an ESATA spindle involved. 

1)  Moving a snapshot file to a different drive for performance reasons.

Hyper-V does not support keeping shapshot difference disks in folders separate from the base disk.  If you’re like me, and store the main VHD on an E-SATA disk, performance isn’t bad, but could be much better if you could move the AVHDx file to a different physical drive, preferably a full speed drive.  This issue is minimized with solid state drives, but if you don’t have one, the best bet is to create your snapshot, remove the disk from your guest VM, move the AVHD file to an accessible location, and then re-attach it.  (Don’t take my word for it:  back stuff up first!)  Once you re-attach, it should run your read/write activities in the avhd, and pull data from the vhd file as needed.

[Edit:  The trick to this part is figuring out where the config files are so you can modify them.  They live at C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\, but the files themselves are XML files with GUID names. 

I typically find the correct one by simply looking at the modified date… it’s probably the one you most recently used. 

Before you can edit these files, you must shutdown the Hyper-V Virtual Machine Management service, and before you start it back up, you’ll have to make sure that the account this service runs with has read/write access to the .avhd, wherever you put it.]

2)  If your E-SATA drive is hardware encrypted, shut off power management.

One issue I was running into that was hosing the host and beating on the guest OS…  My ESATA disk has a keypad that you have to tap out a PIN on in order for the disk to function.  Power management was shutting down that drive at about 20 minutes by default, causing the disk to reset and disconnect, even with a VM running off of it…  this is the computer equivalent of crossing the streams… suffice it to say it’s Bad™.  So using advanced power management, I was able to tell the system to stop spinning down the drives while the power cord was plugged in.  (If I’m on battery, I typically won’t be running my VMs on it.)

Tech in the 603, The Granite State Hacker

My Windows 8 Adventure So Far

I had different reasons for doing each of the upgrades I’ve done so far… Here’s a list of the upgrades I’ve done so far, and why… maybe something I talk about will resonate:

My home PC (desktop):
I upgraded my computer initially to see what it was like. For $40, you don’t even have to go to the store to buy it.  That’s the Pro version, you can buy that to upgrade XP, Vista, and Windows 7 computers…  and it’s a way better price than the $200 upgrade from Windows Vista to Windows 7 for example.   It’s a relatively easy web-based upgrade. (I encountered some quirks with Symantec/Norton anti-virus, but Windows 8 includes security apps which make a good replacement.)  I’ve found it to be as solid as Windows 7, and once you get used to the mouse gestures and the way “Modern UI” works, navigation is easier than it was in Chicago-era UI’s (The UI we’ve basically had since Windows 95).

I also intend to explore app development with it. 

I’m also deciding my hardware upgrade path.  It’s an inexpensive way to get a grip on what devices I might want to invest in, going forward… for example, do I want to upgrade my current PC, which is great but has no touch screen support, lacking virtualization support, or do I want to bite the bullet and get something more current. 

I’m still deciding on a tablet, and it’ll either be “Surface Pro” or “Surface RT”…  I don’t want to invest in iPad or iPhone because they aren’t going to provide the level of integration I’m seeing and liking with the Windows 8 generation of devices.   Right now, I’m actually leaning toward maybe getting a Surface RT tablet, which are already available at Microsoft Stores at the mall…  the Surface Pro will be more fully featured, but cost more.  I’m thinking for what I want to do with a tablet, the RT will suffice, and if I need more horsepower from my tablet, I’ll just remote into a regular computer.
 
My wife’s PC (laptop):
I upgraded my wife’s computer because she & the kids loved the free games they saw me get from the app store… which works a lot like app stores on iPhone, Android and Windows Phone… they all mastered the “Modern UI” the first day, and found it to be an improvement, as well… so she got the “shiny new” experience on her older laptop.

That experience also enabled me to check out how Windows 8 devices (this includes computers, laptops, tablets, and phones) all communicate through the cloud…  I was easily able to transfer my profile from my desktop to my own login on my wife’s laptop.  This is something that Microsoft has been trying to make better for years, and used to only be available to Enterprise users, but now, thanks to the cloud, it’s something anyone can take advantage of.

My sister-in-law’s PC (laptop)
I upgraded my sister in law’s machine to get a less-involved, not quite so technical perspective on it. She picked up the new features right away, and is enjoying things like the free apps… Fresh Paint is one she mentioned as being a favorite, which is one my daughter is particularly fond of, as well.  She’s had an odd behavior with it that I have to fix next time I get the chance, but it’s just that startup takes longer than it should. 

My work machine (laptop)
I upgraded my work machine because I use Virtual Machines a lot, and Windows 8 has an updated version of Hyper-V in it… I was a bit nervous about this at first… my work depends on not screwing up my VMs, but after getting my first VM migrated from VirtualBox, I’m really glad I did it… Results so far are that performance seems better, and it’s just so much more flexible than VirtualBox was.  I’m hoping to see some bugs I encountered in my VirtualBox hosted machines go away too, haven’t tried that just yet…  (Stepping through code was a bit flaky in VirtualBox VMs)

To come…  My Mom’s machine (desktop)
I’m upgrading my mom’s machine for more practical reasons.   I think the UI will be easier for her to get around.  That said, it’s mostly the fact that Windows 8 has better apps included than what she’s using. (For example instant messaging, email, news… the web-based apps she’s using are limited in comparison.)  She’ll be able to toy with the apps from the app store, as well, which will be relatively a new technical freedom for her altogether.

Still…
If you want to see Windows 8 in action without risking a computer, I recommend stopping by a nearby Microsoft Store.  They have them set up so you can spend a bit of time playing with them, and plenty of people around to bounce questions off.

Tech in the 603, The Granite State Hacker

Time to Remodel the Kitchen?

A few good reasons to consider keeping your IT infrastructure up to snuff…

http://edgewatertech.wordpress.com/2012/08/21/time-to-remodel-the-kitchen/

(I’m honored to have the post accepted & published on Edgewater’s blog.)  🙂 

Tech in the 603, The Granite State Hacker

Custom Guid’s

Caught a question from Stacy Draper, @StacyDraper this morning about custom guids, to make them more recognizable.

It reminded me of a post I saw recently about Facebook using hex characters to make IPv6 addresses more recognizable.

Here’s what I was thinking… create a guid that has an embedded word.

For example, the following code creates a Guid that always starts with FACEB00C:

using System;
usingSystem.Collections.Generic;
using System.Linq;
usingSystem.Text;
 
namespaceCustomGuidTest
{
    class Program
    {
        static void Main(string[] args)
        {
            GuidcustomGuid = GenerateCustomGuid();
            Console.WriteLine(customGuid.ToString(“B”));
            Console.ReadKey();
        }
 
        static Guid GenerateCustomGuid()
        {
            Guidresult;
 
            //0xFACEB00C
            //backwards, but required this to achieve desired result
            //Likely due to some standard with respect to bit order.
            byte[] custom = new byte[] { 0x0C, 0xB0, 0xCE, 0xFA  };

            byte[] random = Guid.NewGuid().ToByteArray();
            byte[] final = new byte[16];
            for(int idx = 0; idx <16; idx++)
            {
                if(idx >= custom.Length)
                {
                    final[idx] = random[idx];
                }
                else
                {
                    final[idx] = custom[idx];
                }
            }
            result = newGuid(final);
            returnresult;
        }
    }
}

Example output:
{faceb00c-7828-4e99-8cf5-280e33202670}

Tech in the 603, The Granite State Hacker

Choen the counterBot

I’m pretty excited… both my kids seem interested in computers and what you can do with them. My daughter, especially, is very interested in learning bits and pieces about programming. My son is interested, but not so much in the nuts & bolts of programming. He is interested in numbers and performance, though. They’re both young, though… he’s in 1st grade, and she’s in 2nd.

She’s expressed some interest in programming before. Last time we talked about application design… she got extremely ambitious and drew about twenty pages of “designs” (hand-drawn mock-ups of screens) for games that she wanted to create. (Essentially knock-offs of games she’s played on favorite websites like PBS Kids and Pixie Hollow.)

More recently, she was using my computer, and discovered that when she’s logged into my computer on her account, she has access to Visual Studio Express. When she asked what it was, I told her it was tools to write programs, and she just lit up. Immediately, she wanted to start working on a simple UI that mimics a math program she uses at school.

I started to realize, however, that she needs to learn more of the basics before we dive into a full fledged user interface. I started to think about some of the earliest programs I learned to write.

Back when I was in 4th grade, I got my hands on an Apple II, and one of the first programs I learned to write on it was a program that accepted two numbers (start and finish) and simply scrolled out a count between them.

Nowadays, we have object oriented programming, and C#, but I wanted to try to keep it simple and understandable for her. I decided that we would create a “counter-bot” class, and we would create an instance of it named whatever she wanted it to be named (She decided on Choen (pronounced more like Cohen)).

Today, we got the basics of the counterBot class written, and wrote a short program to create an instance of it, initialize it, and start counting, sending the numbers out to the screen. A countBot has beginAt, finishAt, countBy properties, and Start, Count, GetCurrentNumber and isFinished commands.

Here’s the code:
counterBot.cs:
——————————————————–


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Choen_the_counterBot
{
class counterBot
{
public int beginAt;
public int finishAt;
public int countBy = 1;
private int currentNumber;

public void Start()
{
currentNumber = beginAt;
}

public int GetCurrentNumber()
{
return currentNumber;
}

public void Count()
{
currentNumber = currentNumber + countBy;
}

public bool isFinished()
{
bool result=false;
if (currentNumber > finishAt)
{
result = true;
}
return result;
}

}
}

——————————————————–

and here’s the console Main method in program.cs:
——————————————————–


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Choen_the_counterBot
{
class Program
{
static void Main(string[] args)
{
counterBot Choen = new counterBot();
Choen.beginAt = 0;
Choen.finishAt = 2000000;

Choen.Start();
DateTime startTime = DateTime.Now;

while (!Choen.isFinished())
{
Console.WriteLine(Choen.GetCurrentNumber());
Choen.Count();
}

Console.WriteLine("Finished counting from "
+ Choen.beginAt.ToString()
+ " to "
+ Choen.finishAt.ToString()
+ " by "
+ Choen.countBy
+ "'s in "
+ (DateTime.Now - startTime)
.Seconds.ToString()
+ " seconds!");

Console.ReadKey();
}
}
}

——————————————————–

I got a bit fancy and added the timer code to see how long it took. Together, my son, daughter, and I watched Choen the counterBot count out various scenarios, and got as ambitious as watching it count from zero to two million. In order to get my son’s interest, I added the timer to see how long it took. We discovered that my computer can count from zero to two million, “out loud” (showing the numbers on the screen) in 52 seconds. If we hid the console writes, it could do it in less than a second.

Next time we play with it, we’ll write a graphic UI (a windows form) and hook Choen the counter bot up to it.