-
With Silverlight 4.0 just announced, here are some great posts / helpful tips on getting started with Silverlight in general.
-
Coding4Fun is in attendance at the Microsoft Professional Developer Conference! We are demoing 5 projects that will have full how-to’s and will be open-sourced as well. Drinktendr, Mind Blaster, Laser Graffiti, Augmented Reality, and Wi-Fi Warthogs are powered by .Net, XNA, WPF, Ling2Sql, WiiMote, MSMQ, Power Wheels, a Mind Set and a giant freaking laser!
Drinktendr
Ever get tired of pouring your own drink? Have someone get upset at you since it was too strong or too weak? Well, why not have a machine pour you the perfect beverage every time! Drinktendr uses Linq2Sql and SQL Server to know what drinks it can make with what is on hand. Combine this with WPF; the end user is given a top end experience.
Mind Blaster
With aliens threatening to invade Earth, it’s your job to travel into outer space and destroy the alien threat…with your mind! Wearing a brainwave detecting headset, coupled with head tracking hardware using a wiimote, use only your brain to destroy alien ships before they destroy you and the rest of humanity.
Laser Graffiti
Putting graffiti on something is not a very nice thing to do but what if with a flick of a switch, it was gone. With a projector, an off the shelf webcam and a laser pointer, you can do just that. Aim the laser at the building and start drawing. Turn off the laser and it all goes away! Using WPF and XNA, we can apply a variety of effects including fire, paint, and various particles. This project was influenced by the Graffiti Research Lab.
Augmented Reality
We’ll be showing off two AR games. ARroller and AR Domino Knockdown. ARroller is a single-player “marble game” experienced through a 3DOF-tracked video–see-through head-worn display. The player holds a tracked board on which a virtual marble must be manipulated through a maze of obstacles by tilting and moving the board. AR Domino Knockdown is a two-player first-person-shooter. Players hold 6DOF-tracked UMPCs through which they fire virtual balls at a configuration of virtual dominos on a shared table.
Wi-Fi Warthogs

Go big or go home. After seeing Halo, we decided we had to make some remote control wart hogs to play laser tag with. Using Xbox controllers, a wireless network, and Power Wheel cars, we’ve made a game of laser tag like no other.
-
Charlie Calvert has created great post covering the XNA Role Playing Game Starter Kit from the XNA team.
Charlie talks about the tile engine and the quest engine. The tile engine supports several layers to create a complex final level. The first layer allows you to define a basic landscape or the interior of a building. A second layer allows you to decorate it with trees, chairs, or other objects. A third layer contains your sprites, and a fourth layer defines the boundaries inside which the sprites can move.
The downloads for the RGP Starter Kit are broken out into versions for XNA Game Studio 2.0 and 3.0. There is also a distinction between code that targets Windows and code for the XBox:
Here are some additional links
-
Microsoft just released a Facebo ok client library to help make it easier for everyone to create some interesting applications. The SDK is broken into a few core DLLs, each serving a core purpose. You can download the SDK from Download the SDK.
- Facebook.dll: This is the main assembly that will be used by all applications. This has all the logic to handle communication with the Facebook application. This assembly also has specific support of XAML applications (Silverlight and WPF) to enhance the Facebook platform to make databinding and data caching easier.
- Facebook.Silverlight.dll: This is the Silverlight version of the main assembly that will be used by all Silverlight applications. This has all the logic to handle communication with the Facebook application. This assembly also has specific support of XAML applications to enhance the Facebook platform to make databinding and data caching easier. The REST API in this assembly is Asynchronous only.
- Facebook.Web.dll: This assembly should be used by Canvas applications. The main functionality supported in this assembly is to encapsulate the handshake between the Facebook application and a canvas application (both FBML and IFrame)
- Facebook.Web.Mvc.dll: Provide a support building canvas applications using ASP.NET MVC. Separated from Facebook.Web.dll to avoid all developers from needing to install the MVC bits.
- Facebook.Winforms.dll: This assembly provides support for writing Facebook applications using Winform technology. This provides a Component that wraps the API to make it easier to use from Winforms. This also contains some user controls to help display Facebook data easily.
If you want some helpful starting points, the Facebook Developer Wiki has some help too.
-
Nope, it stands for structured actually. But if you are a bit afraid of SQL, MSDev.com has 20 how-to videos about SQL Server 2008 Express! This series of short how-to-videos will touch on SQL Server Express 2008 features such as: how to backup a SQL Express database, how to backup a remote SQL Express database, how to restore a SQL Express database, how to update SQL Server Express 2005 to SQL Server Express 2008 and more.
-
This article describes a quick and easy application to play Halloween tricks on the PC.

Introduction
This article describes “Gremlin”, a quick and easy application that lets you play Halloween tricks. When your victim's computer is idle, Gremlin moves windows around on the screen, changes the focus window, moves your mouse, scrolls windows, and types nonsensical stuff. When there is background noise – like someone talking – it will shake the screen, even as your victim is typing away.
Deployment
Run it right away. You can download, copy theGremlin.exe executable and NAudio.dll to the victim’s computer (say in c:\ directory), and then double click on the executable to run it.
Run it later. The other option is to copy the executable and dll files (or a shortcut to it) to the Startup folder on the victim's machine and watch the fun begin when they start up their machine in the morning!
If the computer you're using runs on Windows XP the path is:
C:\Documents and Settings\All Users\Start Menu\Programs\Startup
With Vista and Windows 7 the path looks like:
C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Just remember to change USERNAME to the name of the user on your machine.
Commandline
Gremlin is compiled as a windows application, so it won't popup a terminal window if you double click on it, etc. But you can run it from the command line with flags:
-aggressive will make the gremlin's actions more obvious
-help will display the command line options
-name NAME is useful for testing. Gremlin will only use windows with this specific title or from this specific application. (Remember to drop the ".exe" from the application filename)
To stop the program, press CTRL+F2.
And now … the dodgy bits
The overall structure of the program is broken down into three kinds of functionality – actions, triggers and some interstitial glue:
Actions
- The screen-shaker is a window that makes the monitor look like it is shaking. Sort of like a loose cable on the back of the monitor.
- A random event might kick the windows around—either friction will slow them down, or...
- Gremlin will type nonsensical messages from the keyboard. These messages are then sent from a virtual keyboard, or they:
- Move the mouse around, or
- Press the mouse buttons, or
- Randomly switch the focus to another window
Triggers
- An audio module listens for sounds that trigger the module that shakes your screen.
- Otherwise, the software uses a p/invoke to GetLastInputInfo() and waits for the user to be idle for a minute or two. When it detects that the user is inactive, it randomly selects and carries out actions.
Other
- A hidden window receives key press events. I tend to reuse this module a lot, as a way to stop experimental programs that may have made my machine unusable.
For fun, I’ll describe a couple of these modules below.
Screen shaker
My favorite bit is the screen shaker. It grabs a picture of the screens, creates a window that spreads across them, and then moves that image back and forth a few pixels every 30ms or so, with a little bit of random rotation. (Faster computers, of course, get a better effect).
The screen shaker is special in four ways:
- It is a top-level window – no other windows are allowed to go over it.
- This top-level window never becomes the focus window (the window that gets the keyboard events).
- Mouse button events (e.g. clicks and double-clicks) are passed through to the windows and desktop underneath. This gives the illusion that this is the "real" desktop shaking, by allowing a person to click on a button or text that passes the click thru to the real button or text.
- It shakes each of the monitor's screens independently
Some background: to create a window that looks like the shaking screen, I used two classes. The first is ScrShake (in ScreenShake.cs), which derives from the second class UnfocusableForm. I’ll describe ScrShake first.
The screen shaking process requires five instance variables:
- animTimer is a System.Windows.Forms.Timer used to force painting of a new frame on the screen.
- bx, and by are how far the screen has shaken up or down, left or right.
- angle is how much the screen has twisted during shaking.
- screenBitmap is an array of bitmaps, one for each of the monitors.
This creates a window without a border or other trappings, then makes it the topmost window and sets a flag in the method variable ExStyle to ignore mouse clicks. More on this flag in a little while.
C#
public partial class ScrShake : UnfocusableForm
{
public ScrShake(double ShakeCoef, double AngleCoef) : base(true)
{
this.SuspendLayout();
… other setup code …
// This is needed since we're over the whole display, and we don't
// want the other areas to be blurry
TransparencyKey = BackColor = ForeColor = System.Drawing.Color.Fuchsia;
DoubleBuffered = true;
TopMost = true;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
ClientSize = new System.Drawing.Size(300, 300);
Name = "topimage";
ResumeLayout(false);
// This is need to pass mouse clicks thru to lower layers
ExStyle |= (int) WS . EX_TRANSPARENT;
… More code that will be described later…
}
}
When the timer has done a given number of animations, it will call the Stop() method. This will stop the animation, clean it up, and hide the window:
C#
void Stop()
{
animTimer . Stop();
Hide();
screenBitmap = null;
}
When the main loops that start the animated screen shaking process, it calls the Screens() method. This grabs an image of the screens, determines the shape of each monitor, and starts the animation for 10 frames.
C#
internal void Screens()
{
if (Visible)
{
Stop();
return ;
}
// Grab the screens
screenBitmap = Program.GrabScreens();
CountDown = 10;
animTimer.Start();
angle = 0.0f;
… code to display the window and get shape of monitors (see below)…
}
The screen capture portion is in ScreenCapture.cs. The method GrabScreens() creates an individual bitmap for each monitor and returns them as an array.
C#
internal void Screens()
{
if (Visible)
{
Stop();
return ;
}
// Grab the screens
screenBitmap = Program.GrabScreens();
CountDown = 10;
animTimer.Start();
angle = 0.0f;
… code to display the window and get shape of monitors (see below)…
}
The code below determines the bounds of each screen and builds up a size of all of the screens put together. The window will be set to be this size.
C#
Screen[] AllScreens = Screen.AllScreens;
// full width/height of all monitors combined
Rectangle fullSize = AllScreens[0].Bounds;
// find a rectangle that will encompass all monitors on the system
// (assuming the primary monitor is on the left/top!)
for (int i = 1; i < AllScreens.Length && i < screenBitmap.Length; i++)
{
Rectangle Bounds = AllScreens[i].Bounds;
if (Bounds.Left < fullSize.Left)
fullSize.X = Bounds.X;
if (Bounds.Right > fullSize.Right)
fullSize.Width = Bounds.Right - fullSize.X;
if (Bounds.Top < fullSize.Top)
fullSize.Y = Bounds.Y;
if (Bounds.Bottom > fullSize.Bottom)
fullSize.Height = Bounds.Bottom - fullSize.Y;
}
The code to show the window, fill the whole screen, and move it to the top looks like:
C#
Show();
WindowState = FormWindowState.Normal;
// cover all monitors with one gigantic window
Location = new Point(fullSize.Left, fullSize.Top);
Size = new Size(fullSize.Width, fullSize.Height);
// bring it to the top
BringToFront();
The constructor also created a timer that drives the screen shaking effect. The timer has a delegate that randomly selects the angle of rotation and the offset of the image, and triggers a repaint of the window. (The amount of shaking is controlled by two external variables called AngleCoef and ShakeCoef).
C#
animTimer = new System.Windows.Forms.Timer();
animTimer.Tick += delegate(object A, EventArgs E)
{
if (--CountDown < 1)
Stop();
angle += (float)((Program.Rnd.NextDouble() - 0.5) * AngleCoef);
bx = (float)((Program.Rnd.NextDouble() - 0.5) * ShakeCoef);
by = (float)((Program.Rnd.NextDouble() - 0.5) * ShakeCoef);
Invalidate();
};
// Sets the timer interval to 30 milliseconds.
animTimer.Interval = 30;
Although each monitor shifts up & down, left & right by the same amount, and rotates by the same angle, they are painted independently. This gives the illusion that each monitor has a shaky image. Paint the window is using the following code.
C#
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.CompositingQuality = CompositingQuality.HighQuality;
// for each monitor, draw the effect
Screen[] AllScreens = Screen.AllScreens;
for(int i = 0; i < screenBitmap.Length; i++)
{
if (null == screenBitmap[i]) continue;
g.SmoothingMode = SmoothingMode.HighQuality;
// grab the size of the current monitor
Rectangle region = AllScreens[i].Bounds;
double ImWidth = screenBitmap[i].Width *
e.Graphics.DpiX / screenBitmap[i].HorizontalResolution;
double ImHeight= screenBitmap[i].Height *
e.Graphics.DpiY / screenBitmap[i].VerticalResolution;
Matrix m = new Matrix();
m.Translate( (float)(- ImWidth /2),
(float)(- ImHeight /2), MatrixOrder.Append);
// rotate the bitmap about the center
m.RotateAt(angle, new Point(0,0), MatrixOrder.Append);
// center the image no matter what its size is
m.Translate( region.Width /2 - bx,
region.Height/2 - by, MatrixOrder.Append);
// assign our transformation matrix
g.Transform = m;
// draw it
g.DrawImage(screenBitmap[i], region.Left, region.Top);
}
The Form that does nothing!
The ScrShaker class uses a help class called UnfocusableForm (in UnfocusableForm.cs) to create a window that never becomes the focus window – it never receives key presses, etc.
This window has no trappings – no border, no resize gripper, no icon, no minimize / maximize buttons, no title bar.
C#
public partial class UnfocusableForm : Form
{
public UnfocusableForm(bool X) : base()
{
if (X)
{
ControlBox = false;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
ShowInTaskbar = false;
ShowIcon = false;
MinimizeBox = false;
SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
StartPosition = System.Windows.Forms.FormStartPosition.Manual;
}
}
Then it overrides the ShowWithoutActivation method so that the window will not become the active when it is shown.
C#
protected override bool ShowWithoutActivation
{ get { return true; } }
The UnfocusableForm class also overrides the CreateParams() method to set extra styles when creating the window. I haven't found a way to set these styles flexibly. Instead, the UnfocusableForm uses a method variable called ExStyle that allows derived classes to specify what flags they desire. In this case, The ScrShake class used the method variable to set a flag that ignore mouse clicks:
C#
protected override CreateParams CreateParams
{
get
{
CreateParams cp=base.CreateParams;
cp . ExStyle |= ExStyle;
return cp;
}
}
Finally, it captures a couple of events that ask a window if it would like to become the active window. (These usually happen when the user clicks on an inactive window):
C#
internal const int MA_NOACTIVATE = 0x0003;
protected override void WndProc(ref Message m)
{
if (m.Msg == (int) WM.MOUSEACTIVATE)
{
m.Result = (IntPtr) MA_NOACTIVATE;
return;
}
if (m.Msg == (int) WM.FOCUS)
{
m.Result = (IntPtr)1;
return;
}
base.WndProc(ref m);
}
Sending Mouse Events
Sending Mouse Events is simple, but not trivial. The wiki at Pinvoke.net provides the signature to the procedures and structures we need to pass to it. (Note: these structures are a bit different, based on a blog posting by Raymond Chen)
C#
[DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] Inputs, int cbSize);
[DllImport("user32.dll", EntryPoint = "GetMessageExtraInfo", SetLastError = true)]
static extern IntPtr GetMessageExtraInfo();
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
internal INPUTTYPE type;
internal INPUT_UNION i;
}
// This generates the anonymous union
[StructLayout(LayoutKind.Explicit)]
struct INPUT_UNION
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
};
[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public MOUSEEVENTF dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
[Flags]
enum MOUSEEVENTF : int
{
MOVE = 0x01,
LEFTDOWN = 0x02,
LEFTUP = 0x04,
RIGHTDOWN = 0x08,
RIGHTUP = 0x10,
MIDDLEDOWN = 0x20,
MIDDLEUP = 0x40,
ABSOLUTE = 0x8000
}
Each of these pieces go together in a specific way. To do a mouse movement, a mouse event structure needs to be created. This involves setting dwFlags to the kind of mouse event (a relative movement in this case), dx and dy to the number of pixels moved. It is also important to set dwExtraInfo to whatever GetMessageExtraInfo() is set to.
C#
MOUSEINPUT MEvent = new MOUSEINPUT();
MEvent.dwFlags = MOUSEEVENTF.MOVE;
MEvent.dx = Rnd . Next(-8, 8) ;
MEvent.dy = Rnd . Next(-8, 8);
MEvent.dwExtraInfo = GetMessageExtraInfo();
Send(MEvent);
Sending the event takes a few more steps that are all wrapped in a helper methdo called Send(). Send these human interface device events via the SendInput() procedure (earlier). The procedure takes an array of events, for different kinds of devices. We have to create the array, set the event type, copy the event data, and then make the call:
C#
public virtual bool Send(MOUSEINPUT Event)
{
INPUT[] Events = new INPUT[1];
Events[0] . type = INPUTTYPE . MOUSE;
Events[0] . i.mi = MEvent;
return SendInput((uint)Events.Length, Events, Marshal.SizeOf(Events[0])) > 0;
}
From here, sending a mouse button click is pretty straight forward. A mouse click is really a mouse button press event, followed by a mouse button release event. To make things interesting the mouse button is choosen at random. The “ugly” part is that each of the mouse buttons is assigned a bit, so a binary shift is used to convert a button number to its bit. And the button release is yet another bit, so the second message shifts the flags, to move the bit from the “button pressed” state to the “button released state”.
C#
MOUSEINPUT MEvent = new MOUSEINPUT();
MEvent.dwFlags = (MOUSEEVENTF) (1 << (2*Rnd.Next(0, 3)+1)) ;
MEvent.dwExtraInfo = GetMessageExtraInfo ();
Send(MEvent);
MEvent.dwFlags = (MOUSEEVENTF)((int) MEvent.dwFlags << 1);
MEvent.dwExtraInfo = GetMessageExtraInfo ();
Send(MEvent);
Generating deranged text
The gremlin can also type deranged sentences that will appear in editors, if the user left one open. The SendWait() method in the SendKeys class does the typing itself:
C#
SendKeys . SendWait(GenerateSentence());
SendKeys . SendWait("{ENTER}");
![clip_image001[6] clip_image001[6]](http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/HalloweenGremlins_105FE/clip_image001%5B6%5D_thumb.jpg)
Generating the sentences is not difficult; I used a simplistic Markov generator. This generator starts by randomly selecting a word that can start a sentence. The Starts variable is array which holds just these words. Then, in the Transition method, this word is used to find the next word that can be included in the sentence. It does this by using a dictionary, called NonStart, which given the word as a key, will return a list of all the words that can come after it. This process then repeats, appending the words onto the end of a string. It stops if it finds a word that can end a sentence.
C#
static Dictionary<string,string[]> NonStart ;
static string[] Starts;
static Dictionary<string,string> Terminal ;
static string GenerateSentence()
{
StringBuilder SB = new StringBuilder();
string Word = Starts[ Rnd.Next(0, Starts.Length) ];
Transition(SB, Word);
return SB.ToString();
}
static void Transition(StringBuilder SB, string Word)
{
while (true)
{
SB.Append(Word);
SB.Append(' ');
// Look up word after this
string[] Nexts;
if (!NonStart.TryGetValue(Word, out Nexts))
break;
int Idx = Rnd.Next(Terminal . ContainsKey(Word) ? -1 :0, Nexts.Length);
if (Idx < 0)
break;
Word = Nexts[Idx];
}
}
Building these two dictionaries and array is a pretty simple process. The BuildSuffixTree() method takes a string and splits it up into sentences. Then it splits each sentence into (lower case) words. The first word of the sentence goes onto the end of the Starts array. Otherwise, the previous word is used to look up a list in a dictionary, and the current word is appended on to the list. This dictionary will become the NonStarts dictionary. The last word of the sentence is also placed into the Terminal dictionary, to indicate that this might be the end of a sentence.
C#
static void BuildSuffixTree()
{
// First, build up a list of words that start sentence, and transitions
List<string> Starts1 = new List<string>();
Dictionary<string,List<string>> Trans = new Dictionary<string,List<string>>();
foreach(string S1 in S.Split(new char[]{'.','?','!'}))
{
string Prev=null;
foreach(string S2 in S1.Split(new char[]{' ','\n','\r','\t',',',';'}))
{
if (S2 . Length < 1)
continue;
if (null == Prev)
{
Starts1.Add(string.Intern(S2.ToLower()));
Prev = string.Intern(S2.ToLower());
continue;
}
List<string> Nextsa;
if (! Trans.TryGetValue(Prev, out Nextsa))
Trans[Prev] = Nextsa = new List<string>();
Prev = string.Intern(S2.ToLower());
Nextsa.Add(Prev);
}
if (null != Prev)
Terminal[Prev] = Prev;
}
// Next, flatten the list of words that start a sentence
Starts = Starts1.ToArray();
// Flatten the transition table
foreach (string S3 in Trans.Keys)
NonStart[S3] = Trans [S3].ToArray();
}
The things that trigger action
The audio trigger
The audio module (in file AudioTrigger.cs) listens in on all of the microphones. It uses NAudio, with a basic setup and delegate structure swiped from Mark Heath’s article “.NET Audio Recording”. The first difference is that it registers all audio input devices:
C#
static WaveIn[] StartMics()
{
int NumDevices = WaveIn.DeviceCount;
WaveIn[] AudIns = new WaveIn[NumDevices];
for (int waveInDevice = 0; waveInDevice < NumDevices ; waveInDevice++)
{
AudIns[waveInDevice] = new WaveIn();
AudIns[waveInDevice].DeviceNumber = waveInDevice;
AudIns[waveInDevice].DataAvailable += waveIn_DataAvailable;
AudIns[waveInDevice].WaveFormat = new WaveFormat(8000, 1);
AudIns[waveInDevice].StartRecording();
}
return AudIns;
}
The real magic in the trigger is a modified version of the waveIn_DataAvailable delegate. Instead of saving the audio, it checks for any sound activity. It starts by checking to see if any of the sound amplitudes are greater than a pre-defined, very high threshold. Then the samples are squared and summed up, and the result is compared with a threshold. If it exceeds the threshold, the trigger is set. These two are good at catching the kind of sound made when a person is talking, futzing on the desk, or typing.

C#
static double AudioThresh = 0.8;
static double AudioThresh2 = 0.09;
static void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
bool Tr = false;
double Sum2 = 0;
int Count = e.BytesRecorded / 2;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
double Tmp = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
Tmp /= 32768.0;
Sum2 += Tmp*Tmp;
if (Tmp > AudioThresh)
Tr = true;
}
Sum2 /= Count;
// If the Mean-Square is greater than a threshold, set a flag to indicate that noise has happened
if (Tr || Sum2 > AudioThresh2)
Interlocked.Exchange(ref AudioTrigger, 1);
}
This sets a flag – AudioTrigger – which will be seen (and reset) in the main loop, with a bit of code that looks like:
C#
while (0 == Shutdown)
{
// Do some lovely events
Application . DoEvents();
Thread . Sleep (20);
//… do some other stuff ..
// Check for a kick from the sound system
if (0 != AudioTrigger)
{
Interlocked . Exchange(ref AudioTrigger, 0);
…
ScreenShaker . Screens();
continue;
}
//… more checks for the user idle trigger…
}
Waiting for the user to be idle
The check to see if the user is not doing anything, the main loop uses a helper method called LastInputTime(), which returns the number of milliseconds the user did any input activity. The loop ensures that the user has been idle for long enough, and then calls the method that selects random gremlin actions:
C#
uint LastTime = Win32.LastInputTime();
if ((uint) Environment.TickCount < IdleTimeTrigger + LastTime)
continue;
The LastInputTime() helper method uses a p/invoke call to the GetLastInputInfo() API call. The wiki at Pinvoke.net provides the signature to the procedure, and a suitable structure that we need to pass to it:
C#
[DllImport("User32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO LastInfo);
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public uint cbSize;
/// <summary>
/// Number of system tickes
/// </summary>
public uint dwTime;
}
Then, these two structures are wrapped up into the LastInputTime() helper procedure, which does the dirty work of allocating and initializing a structure.
C#
internal static uint LastInputTime()
{
LASTINPUTINFO lastInput=new LASTINPUTINFO();
lastInput.cbSize = (uint)Marshal.SizeOf(lastInput);
GetLastInputInfo(ref lastInput);
return lastInput . dwTime;
}
Conclusion
The tricks gremlin plays are intended to be somewhat subtle. There is an aggressive option to make it react more, and move windows around more visibly.
If you want to try this out, the download link for the executable and source code are at the top of the article!
Resources and References
This article is a blatant grab bag of experimental and reused code from earlier experiments and other demo programs. Below are some of the projects I lifted code examples from:
About The Author
Randall Maas writes firmware for medical devices, and consults in embedded software. Before that he did a lot of other things… like everyone else in the software industry. You can contact him at randym@acm.org.
-
Have you hated how WPF has blurry text depending on what happens? Well, .Net 4.0 and WPF 4.0 fixes that. If you try out a version after Visual Studio 2010 beta 2, you can use these fixes as well. The MSDN Text blog outlines how to fix your text and the why / how of some of the blurriness. A lot comes from display screens not having a high enough DPI and subpixel rendering. But enough excuses and how can you fix it? Here is an example of how to do the look of the picture above.
<StackPanel>
<TextBlock>
Hello World ... Ideal text formatting
</TextBlock>
<TextBlock TextOptions.TextFormattingMode="Display">
Hello World ... Display text formatting
</TextBlock>
</StackPanel>
The blog posting talks about additional settings as well.
-
Over at the Visual Basic blog, Matt Gertz has a great post on hidden effects with loops on stuff you may not realize. He talks about how looping backwards versus forwards depending on the situation will drastically simplify your code or if you’re looping and using the array.Count how the Count property will be called on every pass.
Matt also has another article about additional hidden costs too that is an interesting, quick read.
-
You guys want to try out the newest and greatest Visual Studio Express? The Visual Studio team just released VS 2010 Beta 2 for everyone to try out. It should be out in March, 2010 for the full version as well.
This is beta so be warned. If you do have an issue, please comment / send feedback to the team so they can be resolved.
-
In this episode of the
Coding4Fun Show,
Brian Peek interviews
Jeremiah Morrill who has written
WPF MediaKit, an amazing library to quickly build DirectShow and MediaFoundation media player controls in WPF. It's amazingly powerful and, as a developer, can be used with just a few lines of XAML. In fact, you can create a full-blown DVD player or webcam capture control in your application each with a single XAML tag! Learn all about this great project, how it was written, and how to use it in your own creations.
-
Looking for some XNA help? Shawn Hargreaves’s blog has a bunch of answers questions like anisotropic filtering, mipmapping, and texture filtering.
If you’re not sure what mipmapping is, it is precalculating a copy of an image that is smaller than the original. This way you control the rendering rather than having it computed on the fly!
-
In this article I demonstrate how to record from the microphone in .NET, with support for setting the recording level, trimming noise from the start and end, visualizing the waveform in WPF and converting to MP3.
Audio Recording in .NET
The .NET framework does not provide any direct support for recording audio, so I will make use of the open source NAudio project, which includes wrappers for a number of Windows audio recording APIs.
Note: It is important to point out that .NET is not an appropriate choice for high sample rate and low latency audio recording, such as that found in Digital Audio Workstation software used in recording studios. This is because the .NET garbage collector can interrupt the process at any point. However, for purposes of recording speech from the microphone, the .NET framework is more than capable. By default, NAudio asks the soundcard to give us data every 100ms, which gives plenty of time for the garbage collector to run as well as our own code.
We will make use of the wrappers for the waveIn API’s, as these are the most universally supported, and allow us freedom to choose the sample rate. We will record in mono, 16 bit at 8kHz, which is more than good enough audio quality for speech, and will not overly tax the processor, which is important as we want to visualize the waveform as well.
Choosing a Capture Device
Normally, you will be able to use the default audio capture device without any difficulties, but should you need to offer the user a choice, NAudio will allow you to do so. You can use the WaveIn.DeviceCount and WaveIn.GetDeviceCapabilities to find out how many recording devices are present, and query for their name and number of supported channels.
On my computer, I have a single waveIn device (Microphone Array) until I plug my headset in, at which point, a new device appears and becomes the default (device 0 is always the default).
int waveInDevices = WaveIn.DeviceCount;
for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
{
WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
Console.WriteLine("Device {0}: {1}, {2} channels",
waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
}
This produces the following output on my computer:
Device 0: Microphone / Line In (SigmaTel , 2 channels
Device 1: Microphone Array (SigmaTel High, 2 channels
Unfortunately these device names are truncated because the WAVEINCAPS structure only supports 31 characters. There is a way of getting the full device name, but it is rather convoluted.
Normally, you will choose Device 0 (the default), but if you wish to select a different input device, simply set the DeviceNumber property on your WaveIn object to the desired number.
Checking the Recording Level
The first step in recording is usually to help the user determine if their microphone is working or not. This is especially important if the user has more than one input on their soundcard. The way we achieve this is simply by starting recording and displaying the level of audio detected to the user with a volume meter. The waveIn APIs do not write anything to disk, so no audio is actually being ‘recorded’ at this point, we are simply examining the input level and then throwing the captured audio samples away.
To begin capturing audio from the soundcard, we use the WaveIn class in NAudio. We configure it with the WaveFormat in which we would like to record (in our case 8kHz mono), before calling StartRecording, to start capturing audio from the device.
waveIn = new WaveIn();
waveIn.DeviceNumber = selectedDevice;
waveIn.DataAvailable += waveIn_DataAvailable;
int sampleRate = 8000; // 8 kHz
int channels = 1; // mono
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.StartRecording();
The DataAvailable event handler will notify us whenever a buffer of audio has been returned to us from the sound card. The data comes back as an array of bytes, representing PCM sample data. This is fine if we are planning to write the audio directly to disk, but what if we wish to have a look at the audio data itself? Each audio sample is 16 bits, i.e. two bytes, meaning that we will need to convert pairs of bytes into shorts to be able to make sense of the data.
Note: if we were recording in stereo, the 16 bit samples would themselves come in pairs, first the left sample, then the right sample.
The following code shows how we might process the raw bytes in the DataAvailable event, and read the individual audio samples out. Notice that we use the BytesRecorded field, not the buffer’s Length property. Also, I have chosen to convert the samples to 32 bit floating point format and scaled them so the maximum volume is 1.0f. This makes processing them through effects and visualizing them much easier.
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) |
e.Buffer[index + 0]);
float sample32 = sample / 32768f;
ProcessSample(sample32);
}
}
Note: One complication of using the waveIn and waveOut APIs is deciding on a callback mechanism. NAudio offers three options. First is function callbacks. This means that the waveIn API is given a (pinned) function pointer which it calls back onto. This means that your DataAvailable callback will come in on a background thread. In some ways this is the cleanest approach, but you need to beware of rogue soundcard drivers that can hang in calls to waveOutReset when using function callbacks (the SoundMAX chipset found on a lot of laptops is particularly prone to this problem).
The second is to supply a window handle. The waveIn APIs will post a message back to be handled on the message queue of that window handle. This method tends to be the most reliable and most commonly used. One gotcha to watch out for is that if you stop recording and immediately restart, a message from the old recording session could get handled in the new session resulting in a nasty exception.
The third is to let NAudio create its own new window and post messages to that. This gets round any danger of messages from one recording session getting muddled up with another. This is the callback method that NAudio will use by default if you call the default WaveIn constructor. But don’t use this from a background thread or from a console application, or the new window that NAudio creates won’t actually get round to processing its message queue.
Visualizing the Recording Level
We have seen how we can begin to capture audio from the soundcard for the purposes of checking the recording level. Now we need to give the user some visual feedback. We will use WPF for our sample recording application. The simplest control we have available to display a single numeric value graphically is the ProgressBar. And because it is WPF, we can fully customize the graphical appearance of the progress bar to look a little more like a volume meter. I have used a gradient going from green to red to show the current volume level. You can read more about how I created this ProgressBar template here.
Figure 1 - A Progress Bar Showing the Current Microphone Volume Level
To help provide the volume level to display, I have created a SampleAggregator class. This is passed every audio sample value we receive and keeps track of the maximum and minimum values. Then, after a specified number of samples, it raises an event allowing the GUI components to respond. We need to be careful not to raise too many of these events or performance will be badly affected. I am raising one every 800 samples, meaning we get 10 updates per second to the screen.
Because I am using data binding, when one of these updates fires, I must raise a PropertyChangedEvent on my DataContext object (also known as the “ViewModel” in the MVVM pattern). Here’s the XAML syntax for binding to my CurrentInputLevel property:
<ProgressBar Orientation="Horizontal"
Value="{Binding CurrentInputLevel, Mode=OneWay}"
Height="20" />
And here’s the code in the ViewModel that ensures that the GUI updates whenever we calculate a new maximum input level:
private float lastPeak;
void recorder_MaximumCalculated(object sender, MaxSampleEventArgs e)
{
lastPeak = Math.Max(e.MaxSample, Math.Abs(e.MinSample));
RaisePropertyChangedEvent("CurrentInputLevel");
}
// multiply by 100 because the Progress bar's default maximum value is 100
public float CurrentInputLevel { get { return lastPeak * 100; } }
Note: Model View ViewModel (MVVM) is a pattern that is growing in popularity amongst WPF and Silverlight developers. The basic idea is that you have no code behind whatsoever on your View (i.e. your xaml markup file), and simply specify all communications with your business logic by means of data binding. The ViewModel serves as an adapter to ease the process of data binding. This approach gives very good separation of appearance and behavior. For the most part, this pattern works very well, but there are a few tricky areas, for which you will need to either write a few lines of code behind, or make use of some cunning tricks such as attached dependency properties or custom triggers. There are several excellent open source helper libraries that can take some of the work out of getting an MVVM application up and running. Have a look here for a comprehensive list.
Adjusting the Recording Level
Suppose the current input level is too high or too soft. We would like to be able to support modifying the recording level. Again, we would like to use data binding to do so, so we will add a volume slider to our XAML:
<Slider Orientation="Horizontal"
Value="{Binding MicrophoneLevel, Mode=TwoWay}"
Maximum="100"
Margin="5" />
Now we have to get hold of the MixerLine that will allow us to access the input volume control for our waveIn device. This requires us to make use of the Windows mixer APIs, which also have wrappers in NAudio. Getting hold of this volume control is not always as straightforward as you might hope (and can require different approaches for XP and Vista), but the following is code that seems to work on most systems:
private void TryGetVolumeControl()
{
int waveInDeviceNumber = 0;
var mixerLine = new MixerLine((IntPtr)waveInDeviceNumber,
0, MixerFlags.WaveIn);
foreach (var control in mixerLine.Controls)
{
if (control.ControlType == MixerControlType.Volume)
{
volumeControl = control as UnsignedMixerControl;
break;
}
}
}
Now we can use the Percent property on the UnsignedMixerControl to set volume to a value anywhere between 0 and 100.
Starting Recording
Now we have got our recording levels set up correctly, we are ready to actually start recording. But since we have already opened our waveIn device, all we need to do is start writing the data we have received into a file.
NAudio has a class called WaveFileWriter which will allow us to write our recorded data to a file. For now, we will write it to a temporary file in PCM format, and convert it later into a better compressed format such as MP3. The following code creates a new WAV file:
writer = new WaveFileWriter(waveFileName, recordingFormat);
Now we can write to the file as we receive notifications from the waveIn device:
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (recordingState == RecordingState.Recording)
writer.WriteData(e.Buffer, 0, e.BytesRecorded);
// ...
}
Note: There are three main options for how to store audio while it is being recorded. First, you can write it to a MemoryStream. This saves the inconvenience of dealing with a temporary file, but you need to be careful not to run out of memory. Also, if your recording program crashes half way through, you have lost everything. At the sample rate we are using for this demo, one minute of audio takes just under 1 MB of memory, but if you were recording at 44.1kHz stereo (the standard for music), you would need about 10 MB per minute.
Second, you can write to a temporary WAV file to be converted to another format later, as we are doing here. While this is not a disk space efficient format, it is very easy to work with, and particularly useful if you are planning to apply any effects or edit the audio in any way after recording.
Third, you can pass the audio directly to an encoder (such as WMA or MP3) as it is being recorded. This might be the best option if you are making a longer recording, and have no need to edit it after recording.
Stopping Recording
Obviously we will stop when the user clicks the stop recording button, but we might also want to set a maximum recording duration to stop the user inadvertently filling up their hard disk. For this example, we will allow one minute of recording.
long maxFileLength = this.recordingFormat.AverageBytesPerSecond * 60;
int toWrite = (int)Math.Min(maxFileLength - writer.Length, bytesRecorded);
if (toWrite > 0)
writer.WriteData(buffer, 0, bytesRecorded);
else
Stop();
Note: Something that can be slightly confusing for users is that when using window callbacks with WaveIn, the last bit of audio you recorded comes in after you have asked recording to stop, so make sure you don’t close the file you are saving to until you have got all the audio back. The FinishedRecording event on the WaveIn object will help you determine when it is safe to close the WaveFileWriter and clean up your resources.
Visualizing the Wave Form
It is often desirable to display the audio waveform to the user. Displaying the waveform while you are recording is sometimes called “confidence recording”, because it allows you to see that audio is being recorded as expected and the levels are still right.
There are a variety of possible approaches for drawing audio waveforms. The simplest is to draw a vertical line showing the minimum and maximum values every time our sample aggregator fires:
Figure 2 - Audio Waveform using vertical lines
At first glance it may seem that this would be trivial to implement in WPF, but there is a real danger of consuming too many resources. For example, simply adding a new line to a Canvas every time a new maximum sample is calculated performs very badly, so it is better to have a fixed number of vertical lines and resize them dynamically.
Another approach is to create a polygon. This requires us to add two points to a Polygon’s Points collection every time we receive a new sample. The trick is to add these points in the middle of the Points collection, rather than at the end, so that the end result is a single shape. This means our waveform can have a different outline color and fill color. To stop the edges from appearing too jagged, we plot points two units apart along on the X axis.
Figure 3 - Audio Waveform rendered using a Polygon
Like the microphone volume meter, the waveform drawing control needs to receive several notifications a second of the maximum and minimum sample values received by the SampleAggregator. When each sample value is received, we either insert new points into our polygon, or, if the whole screen is full, we go back to the left-hand edge and continue drawing from there.
For the confidence recording display I have used the Polygon method, which is in a class called PolygonWaveFormControl. Here’s the code which calculates the new points or updated point locations as we receive a new maximum sample:
public void AddValue(float maxValue, float minValue)
{
int visiblePixels = (int)(ActualWidth / xScale);
if (visiblePixels > 0)
{
CreatePoint(maxValue, minValue);
if (renderPosition > visiblePixels)
{
renderPosition = 0;
}
int erasePosition = (renderPosition + blankZone) % visiblePixels;
if (erasePosition < Points)
{
double yPos = SampleToYPosition(0);
waveForm.Points[erasePosition] =
new Point(erasePosition * xScale, yPos);
waveForm.Points[BottomPointIndex(erasePosition)] =
new Point(erasePosition * xScale, yPos);
}
}
}
private void CreatePoint(float topValue, float bottomValue)
{
double topYPos = SampleToYPosition(topValue);
double bottomYPos = SampleToYPosition(bottomValue);
double xPos = renderPosition * xScale;
if (renderPosition >= Points)
{
int insertPos = Points;
waveForm.Points.Insert(insertPos, new Point(xPos, topYPos));
waveForm.Points.Insert(insertPos + 1, new Point(xPos, bottomYPos));
}
else
{
waveForm.Points[renderPosition] = new Point(xPos, topYPos);
waveForm.Points[BottomPointIndex(renderPosition)] =
new Point(xPos, bottomYPos);
}
renderPosition++;
}
The erase position calculation is to blank out some previous sample values to make it obvious where the new data is appearing after we have wrapped around once:
Figure 4 PolygonWaveForm control's “blank zone”
Note: There are faster ways to perform rendering in WPF. One option is to use the WriteableBitmap class and draw directly onto it. This could be a good approach if you were using the vertical lines method of rendering. The second is to use DrawingVisual objects, which are lightweight drawing objects offering better performance than using classes derived from Shape. The down-side is the loss of features such as DataBinding and the ability to fully describe the picture in XAML, but for WaveForm drawing this is not really a drawback. I use the DrawingVisual method in the Save Audio part of this application.
Another challenge was how the waveform drawing control could receive notifications since I am using MVVM so I have no direct access to the SampleAggregator. A simple way around this was to create a Dependency Property on PolygonWaveFormControl:
public static readonly DependencyProperty SampleAggregatorProperty =
DependencyProperty.Register(
"SampleAggregator",
typeof(SampleAggregator),
typeof(PolygonWaveFormControl),
new PropertyMetadata(null, OnSampleAggregatorChanged));
public SampleAggregator SampleAggregator
{
get { return (SampleAggregator)this.GetValue(SampleAggregatorProperty); }
set { this.SetValue(SampleAggregatorProperty, value); }
}
private static void OnSampleAggregatorChanged(object sender, DependencyPropertyChangedEventArgs e)
{
PolygonWaveFormControl control = (PolygonWaveFormControl)sender;
control.Subscribe();
}
This allows us to bind the PolygonWaveFormControl to the SampleAggregator made public on our DataContext:
<my:PolygonWaveFormControl
Height="40"
SampleAggregator="{Binding SampleAggregator}" />
Trimming the Audio
We have created a temporary WAV file, but before the user saves it to a file of their choosing, we want to allow them to trim off any unwanted parts from the start and end of the recording. To do this I would like to display the entire recorded waveform, with a selection rectangle superimposed on top to allow a sub-range to be selected.
Figure 5 - GUI to allow selection of a portion of the recorded audio
To accomplish this kind of interface we need three components. The first is a ScrollViewer. The ScrollViewer allows us to scroll left and right through the WaveForm if it is too big to fit onto a screen, which is likely if you record more than a few seconds of audio.
The second is a new type of WaveForm renderer that will render an entire file, rather than my PolygonWaveFormControl which started again at the left when the screen filled up. For this I created WaveFormVisual which uses DrawingVisual objects to draw the entire WaveForm. Obviously if we wanted to record for a long period, this approach would need to be optimised as the polygon it creates would have thousands of points, but for short recordings, it works fine.
The third piece was the hardest to get right – the selection rectangle to support mouse dragging selection of the waveform. For this I created the RangeSelectionControl.
The RangeSelectionControl is simply a blue rectangle with a solid outline and semi-transparent fill sitting on a Canvas. The magic occurs in the mouse handler. We need to detect when the user hovers over the left or right edge of the rectangle, and set the cursor to show a horizontal resizing icon. This can be done in the MouseMove event, checking the X coordinate and then setting the Cursor property:
Cursor = Cursors.SizeWE;
When the user clicks the left-button while over the edge, we begin to drag. Key to this is calling Canvas.CaptureMouse. If we don’t do this, as soon as you try to drag the rectangle bigger, the mouse move events are lost to other controls underneath.
void RangeSelectionControl_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point position = e.GetPosition(this);
Edge edge = EdgeAtPosition(position.X);
DragEdge = edge;
if (DragEdge != Edge.None)
{
mainCanvas.CaptureMouse();
}
}
}
Now in the MouseMove methods, we can change the Canvas.Left and Width properties of the rectangle to resize it.
The ScrollViewer is quite straightforward to use, but you must remember to set CanContentScroll property to true, and also to set the size of the items within the ScrollViewer correctly.
<ScrollViewer CanContentScroll="True"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Hidden">
<Grid>
<my:WaveFormVisual Height="100"
HorizontalAlignment="Left"
x:Name="waveFormRenderer"/>
<my:RangeSelectionControl
HorizontalAlignment="Left"
x:Name="rangeSelection" />
</Grid>
</ScrollViewer>
We set the appropriate Width of the WaveFormVisual and RangeSelectionControl based on the total number of points we have drawn in the waveform.
Saving the Audio
So we are finally ready to save the audio. We will offer the user two choices of format to save in. The first is simply to save as a WAV file. If the user has selected the entire recording, we only need to copy the audio across to their desired location. If, however, the user has selected a sub-range, then we need to trim the WAV file. This can be quickly accomplished using a TrimWavFile utility function that copies from a WAV file reader to a WAV file writer, skipping over a certain number of bytes from the beginning and end.
public static void TrimWavFile(string inPath, string outPath,
TimeSpan cutFromStart, TimeSpan cutFromEnd)
{
using (WaveFileReader reader = new WaveFileReader(inPath))
{
using (WaveFileWriter writer =
new WaveFileWriter(outPath, reader.WaveFormat))
{
int bytesPerMillisecond =
reader.WaveFormat.AverageBytesPerSecond / 1000;
int startPos = (int)cutFromStart.TotalMilliseconds *
bytesPerMillisecond;
startPos = startPos - startPos % reader.WaveFormat.BlockAlign;
int endBytes = (int)cutFromEnd.TotalMilliseconds *
bytesPerMillisecond;
endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
int endPos = (int)reader.Length - endBytes;
TrimWavFile(reader, writer, startPos, endPos);
}
}
}
private static void TrimWavFile(WaveFileReader reader,
WaveFileWriter writer, int startPos, int endPos)
{
reader.Position = startPos;
byte[] buffer = new byte[1024];
while (reader.Position < endPos)
{
int bytesRequired = (int)(endPos - reader.Position);
if (bytesRequired > 0)
{
int bytesToRead = Math.Min(bytesRequired, buffer.Length);
int bytesRead = reader.Read(buffer, 0, bytesToRead);
if (bytesRead > 0)
{
writer.WriteData(buffer, 0, bytesRead);
}
}
}
}
We also want to offer the ability to save as MP3. The easiest way to create MP3 files is to use the open source LAME MP3 encoder (do a web search for lame.exe to get hold of this application if you haven’t already got it). Our application will look in the current directory, and prompt the user to find lame.exe if it is not present, as we do not include it in the application download. Assuming you do provide a valid path, we can then convert our (trimmed) WAV file to MP3 by simply calling lame.exe with the appropriate parameters.
public static void ConvertToMp3(string lameExePath,
string waveFile, string mp3File)
{
Process converter = Process.Start(lameExePath, "-V2 \"" + waveFile
+ "\" \"" + mp3File + "\"");
converter.WaitForExit();
}
We end up with a nice compact MP3 file containing the selected portion of our microphone recording.
Exploring the Sample Code Solution
The main WPF sample application is found in the VoiceRecorder project. This contains the main window along with the three views and their associated ViewModels. VoiceRecorder.Core contains some WPF helper classes and user controls to help with the plumbing and GUI of the application, while VoiceRecorder.Audio contains the classes that actually perform the recording, editing and converting of audio.
About the Author
Mark Heath is a software developer currently working for NICE CTI Systems in Southampton, UK. He specializes in .NET development with a particular focus on client side technologies and audio playback. He blogs about audio, WPF, Silverlight and software engineering best practices at http://mark-dot-net.blogspot.com. He is the author of several open source projects hosted at CodePlex, including NAudio, a low-level .NET audio toolkit (http://www.codeplex.com/naudio).
-
Simon Hoade liked how Windows had 3D photo stacks and wanted to recreate the effect.
He shows how he used threads and calling WPF code from a web server without actually having a WPF application!
He mentioned he used two tutorials also that helped aid him.
-
Rick Barraza has 3 amazing examples in Silverlight.
- Writable Bitmaps
This example shows how to use pixel shader displacement with line drawing to create some amazing effects. - Fluid Dynamics
Rick talks about how to do dynamic visuals, why they matter. - Vector Fields
This example shows how to gray scale an image, do edge detection then use that information to draw vectors on the image for additional effects.
If you want additional information with Rick, Brian Peek did an interview with him regarding Silverlight advance rendering techniques as well.
-
If you have a PowerShell script that you need run from your application and didn’t know quite how to accomplish the feat, Cody Batt has an example of doing this at DevX. Cody goes in and talks about about calling PowerShell with a quick example of getting the running processes.
The process of being able to do this may make a few feel a bit uncomfortable since you need to manually go in and alter your project file in notepad and add in a reference to System.Management.Automation.
If you want to check out more about PowerShell, the following sites should help out: http://powershell.com/cs/ps/overview.aspx, http://thepowershellguy.com/blogs/posh/, http://blogs.msdn.com/powershell/.