Archive for the ‘C#’ Category

This weekend I watched the rather excellent video series called Message Queue Fundamentals in .NET on Pluralsight. Elton (the author of the video) uses LINQPad to demonstrate concepts and code throughout the video lessons. Up to this point I had never even heard of it  – but I was impressed!

“But what does it do!?” I hear you shout. It is a lightweight application where you can write C#/VB/F# statements (or console application-esque programs) and execute them within the application. This doesn’t sound that great but it’s actually very powerful and cool.

It comes with built in support for referencing the .NET Base Class Libraries (BCL) – just hit F4 to add a reference. Naturally you can also add references to any dll that you like also. This makes an excellent tool for “trying something out” without the pain of creating a new console application.

Like SQL Server Management Studio you can highlight code you want to execute, hit play and it will only execute the highlighted code. You can use the Dump() extension method to output any object to the output.

You can also read input from user like you can a console application. At some point I am sure I will find some great use for this…what that is I don’t know!!


You can even execute multiple programs at once – in the video tutorial Elton has multiple applications sending and listening for messages simultaneously.

Think about it – you no longer need to create a console application just to try out a bit of code – or to write that adhoc program to <insert some menial task> that will go in the bin shortly after. You can easily organise and save any code you write too!

Okay, so if that hasn’t whet your appetite then maybe this will…you can use LINQ to SQL to query your database! LINQPad have even layed down the gauntlet with the LINQPad challenge where they are encouraging you to replace SQL Server Management studio…a challenge that I have accepted!


How much does it cost? Well it’s free with 1 tiny, little, small, incey-wincey drawback…the free version doesn’t come with intellisense. I am (perhaps overly) excited by this and have (literally) just purchased a license.


By no means is LINQPad going to replace Visual Studio – it’s not even trying too, but it may be useful for the times where you just want to write some code. I am also sold on the ability to query a database with LINQ, and with support for SQL for those unfortunate times where you have no choice I probably will use SSMS less often.

Here is simple code example showing user input.

 // Simple interactive app
void Main()
	var user = new
		Name = ScreenHelper.GetUsersName(),
		Age = ScreenHelper.GetUserAge()


public static class ScreenHelper
	public static string GetUsersName()
		Console.WriteLine("What is your name?");
		return Console.ReadLine();

	public static int GetUserAge()
		while (true)
			Console.WriteLine("What is your age?");
			var input = Console.ReadLine();

			int age;
			if (int.TryParse(input, out age))
				return age;

I have spent the past couple of hours trying to make a sound effect play quickly and correctly using the MediaElement. I was tring to play the sound effects using the following code:

var snd = new MediaElement {AutoPlay = false, Volume = 60};
var folder = await Package.Current.InstalledLocation.GetFolderAsync(@"Assets\Sounds");
var file = await folder.GetFileAsync("shot.wav");
var stream = await file.OpenAsync(FileAccessMode.Read);
snd.SetSource(stream, file.ContentType);

If I attempt to play a single MediaElement repeatedly and quickly, it sometimes (often) doesn’t play. Creating multiple MediaElements and trying to play them in quick succession is a mixed bag, sometimes they play correctly, sometimes they don’t play, and sometimes they save themselves up and all play at once!

MediaElement just doesn’t cut it.

After some searching on the Internet, it would seem that SharpDx is the answer! I have heard of it before but never used it.

The power of DirectX for .NETSharpDX is an open-source project delivering the full DirectX API under the .Net platform, allowing the development of high performance game, 2D and 3D graphics rendering as well as realtime sound application.

In VS2012 choose Tools > Library Package Manager > Package Manager Console.

Type: Install-Package SharpDX.XAudio2

If you get an error make sure that NuGet doesn’t need any updates!

I created a new class called SoundEffect and gave it the following variables:

readonly XAudio2 _xaudio;
readonly WaveFormat _waveFormat;
readonly AudioBuffer _buffer;
readonly SoundStream _soundstream;

And initiate them with a constructor:

public SoundEffect(string soundFxPath)
_xaudio = new XAudio2();
var masteringsound = new MasteringVoice(_xaudio);

var nativefilestream = new NativeFileStream(

_soundstream = new SoundStream(nativefilestream);
_waveFormat = _soundstream.Format;
_buffer = new AudioBuffer
Stream = _soundstream.ToDataStream(),
AudioBytes = (int) _soundstream.Length,
Flags = BufferFlags.EndOfStream

Then to play the sound effect I simply created a Play() method:

public void Play()
var sourceVoice = new SourceVoice(_xaudio, _waveFormat, true);
sourceVoice.SubmitSourceBuffer(_buffer, _soundstream.DecodedPacketsInfo);

I create a new instance of the SoundEffect class:
private SoundEffect shotEffect = new SoundEffect(@"Assets\Sounds\shot.wav");

And then simply play it:
This works perfectly 😀