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!!

input1input2output

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!

query

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.

Conclusion

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()
	};

	user.Dump();
}

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;
			}
		}
	}
}
 
Advertisements

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);
snd.Play();

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(
soundFxPath,
NativeFileMode.Open,
NativeFileAccess.Read,
NativeFileShare.Read);

_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);
sourceVoice.Start();
}

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

And then simply play it:
shotEffect.Play();
 
This works perfectly 😀

Random flights to Prague

Posted: October 22, 2012 in My World
Tags: ,

Next week I am going to Prague for 4 nights! A lovely last minute booking, which has particuarly random flights…

For some reason we are going…

London Heathrow > Oslo > Prague

and on the way back

Prague > Frankfurt > London Heathrow

Random Route

I can understand stopping at Frankfurt as its on the way, but Oslo is really random! Oh well, one more place to *very* briefly visit!

Fly in an ice cube!

Posted: October 22, 2012 in My World
Tags: , , ,

An actual fly in an actual ice cube! I think this is the first time *in all my years* that I have witnessed such things..it would seem that its true and it can actually happen. This wasn’t even a funny prank…
Fly got iced

Car Cam has arrived

Posted: October 20, 2012 in Uncategorized
Tags: , ,

Cars driving at me on the wrong side of the road.
Teenagers jumping out in front of my car because they dropped their phone.
Lorries changing lane without looking and almost running me off of the road.

It would seem that driving to work everyday is a dangerous game. At least 2 “brown trouser moments” a week. (especially on Thursdays for some reason)

Car Cam

The eye in the…car…

I am now the proud new owner of a car cam. £20 from Amazon: http://www.amazon.co.uk/gp/product/B0056WFPCK/ref=oh_details_o00_s00_i00

I have had a little go and it was actually pretty good despite the price…let’s see what I can capture…

I’m glad that I checked my email this evening, it looks as if I’ve had a bit of a result!

Image

There’s no way this could be fake with the email address: go4claims@yahoo.es

Edit: I posted this on facebook and some people didn’t seem to get that its fake! Always remember

Steve’s online safety tip #307 – Don’t reply or click links from random emails!

Aside  —  Posted: October 19, 2012 in My World
Tags: , ,

Wheres my activation key?

Posted: October 19, 2012 in My World
Tags: , ,

No Key :(

A couple of weeks ago I bought Rocksmith for Xbox 360. This has genuinely fuelled my interest and passion for playing the guitar again (I play on and off, but I’m not that great). For those of you who don’t know, its basically guitar hero but using a real guitar. As you are using a real guitar you actually learn to play the songs, it’s like a band in a box!

Sadly I don’t get to play it everyday, my Xbox is connected to my TV in the front room of which I have restricted access (somebody is usually watching some rubbish TV). I decided to buy the PC version that came out today so that I can install it on my Laptop and play anywhere and hopefully every day! During the install process it asks you for your activation key…WHAT…ITS MISSING! :@

It turns out that they have failed to stick activation keys in any of the PC copies in Europe…somebody is going to get fired!

http://ubisoft-en.custhelp.com/app/answers/detail/a_id/17223/~/missing-cd-key-for-rocksmith-pc.