Jesper Niedermann's .NET Blog
about .NET and related technologies RSS 2.0
# Sunday, 13 June 2010

Imagine that you want to make an extension for Visual Studio 2010 that creates new custom right click menus for the Solution Explorer. Imagine that you could do it by just implementing a good old .NET interface like this:

public class MenuManager : IMenuManager
public IEnumerable<IMenuItem> GetMenus(ContextLevels menuForLevel)
var menuItems = new List<IMenuItem>();
var menuItem1 = new MenuItem("My Menu1");
var menuItem2 = new MenuItem("My Menu2");
return menuItems;

public string MainMenu()
return "My Main Menu";


Well it turns out you can. In MME I have helped you do exactly that. No more using the convoluted add-in model of Visual Studio to accomplish this goal. And it is also much simpler than using GAX/GAT (that can do so much more to be fair).

You can easily install MME, either by downloading directly from Codeplex or by installing directly from the Extension Manager in Visual Studio 2010 (you can find it under the Tools menu).

MME does not work for the Express editions of VS.

I also recommend installing the MME MenuManager template which you can also find in the Extension Manager.

At codeplex you can read more about implementing and deploying MME’s and also get further insight on the architecture.

Sunday, 13 June 2010 18:20:02 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Tools | Visual Studio
# Wednesday, 12 May 2010

Windows developers who are used to click events in WinForms, WPF or Silverlight might miss click and double click events, but because of the game loop where everything is drawn and updated every few milliseconds an event based approach is probably not a good idea in most cases. Not so much because of the performance of an event based approach, but more because the complexity is overwhelming. If several users clicks and double clicks several keys on the keyboard simultaneously, how would you decide which of these are clicked and in which order, and further more it is not obvious to subscribers of the events that they are in the game loop, so they might do code which performs inadequately.

I have made a few generic classes that expands the MouseState, GamePadState and KeyboardState in order to make click “events” available. But of course I utilize the standard polling mechanism in XNA which is to call GetState() on each device.

The approach is that I call my own version of GetState() in the main game loop. My GetState() method first enqueues the state in a Queue, and then dequeues all states older than 500 ms, before the state is returned. In this way a historical map of user interactions is maintained at all times.

Now I can simply check if a key or button was clicked by checking if the key was pressed and then released “historically”. Similarly I can check for double click by checking if the key or button was pressed, then released, then pressed again and then released again.

Now you probably realize why I go back 500 ms. It is because this is the standard time in which a double click should be executed.

I have used this approach in the game Protect the Carrot at and it works like a charm (The url for the game does not work today, since the game will only be published in a few weeks).

Here is the implementation of the MouseExtended class which uses this approach:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace PTC.Input
public class MouseExtended : InputDeviceExtended<MouseState>
private static MouseExtended m_Current;
public static MouseExtended Current
if (m_Current == null)
m_Current = new MouseExtended();
return m_Current;

public MouseState GetState(GameTime currentTime)
MouseState state = Mouse.GetState();
EnqueueNewState(currentTime, state);
return state;

private bool ClickCount(MouseButton checkButton, int requiredCount)
ButtonState found = ButtonState.Released;
int count = 0;
foreach (InputStateExtended<MouseState> stateExt in RecordedStates)
if (found == ButtonState.Pressed &&
ButtonStateToCheck(stateExt.State, checkButton) == ButtonState.Released)
if (count >= requiredCount)
return true;
found = ButtonStateToCheck(stateExt.State, checkButton);
return false;

private ButtonState ButtonStateToCheck(MouseState state, MouseButton checkButton)
switch (checkButton)
case MouseButton.Left:
return state.LeftButton;
case MouseButton.Middle:
return state.MiddleButton;
case MouseButton.Right:
return state.RightButton;
case MouseButton.XButton1:
return state.XButton1;
case MouseButton.XButton2:
return state.XButton2;
return state.LeftButton;

public bool WasSingleClick(MouseButton checkButton)
return(ClickCount(checkButton, 1));

public bool WasDoubleClick(MouseButton checkButton)
return (ClickCount(checkButton, 2));


And here is the generic InputDeviceExtended class which MouseExtended inherits:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PTC.Input
public class InputDeviceExtended<S> where S : struct
private Queue<InputStateExtended<S>> m_RecordedStates = new Queue<InputStateExtended<S>>();

public Queue<InputStateExtended<S>> RecordedStates
get { return m_RecordedStates; }

private Stack<InputStateExtended<S>> m_StatesForReuse = new Stack<InputStateExtended<S>>();

protected void EnqueueNewState(GameTime time, S state)
if (!state.Equals(m_CurrentState))
m_CurrentState = state;
m_RecordedStates.Enqueue(CreateState(time, state));

private S m_CurrentState;
public S CurrentState
get { return m_CurrentState; }

protected void DequeueOldStates(GameTime currentTime)
InputStateExtended<S> state = null;
if (m_RecordedStates.Count > 0)
state = m_RecordedStates.Peek();
if (state != null && state.StateTime < currentTime.TotalRealTime.Subtract(new TimeSpan(0, 0, 0, 0, InputDeviceConstants.ClickCountTimeMS)))

private InputStateExtended<S> CreateState(GameTime time, S state)
if (m_StatesForReuse.Count > 0)
//Reuses the object to fight of the GC
InputStateExtended<S> stateExt = m_StatesForReuse.Pop();
stateExt.StateTime = time.TotalRealTime;
stateExt.State = state;
return stateExt;
return new InputStateExtended<S>(time, state);

Notice that the Recorded States are reused. When dequeued from the queue the are added to a reuse stack. This is a standard trick to fight of the Garbage Collector, by always keeping a reference to objects on the heap they never become garbage + they are reused so the memory use will not explode.

I have also implemented the necessary extended classes for the Keyboard and the gamepad. I have included them in the attachment to this post. I have not tested the GamepadExtended class since I do not own a Gamepad, but it is implemented exactly as the keyboard and mouse classes and ought to work.

To wire up the new classes you just add the relevant Getstate() calls to the Update game loop like so:

protected override void Update(GameTime gameTime)

And then you can check for click and double click “events”. For instance you check for double click of the left mouse button like this:

//Do double click reaction

Hope you like the stuff.And look out for the amazing “Protect The Carrot” game within the next month or so, at

In I have included the code for all the InputDevice classes.

Wednesday, 12 May 2010 21:38:51 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET | Tips & Tricks | XNA
# Sunday, 07 March 2010

In Microsoft CRM you have no choice when doing updates to the database. You have to use the CRM Web Service.

Microsoft does not support direct SQL updates of the underlying SQL Server tables, because of the behind the scenes CRM magic.

The Web Service is OK for small amounts of data. But recently I had a case where I had to code an import of a spreadsheet with up to 100.000 rows into CRM.

This would result in 600.000 web service calls, since there was 5 retrieves for each updates. The first tests showed that each web service call would be about 1,5 seconds even though they were done internally on the CRM server a fast quad-core 64 bit machine. This would be 900.000 seconds or 250 hours, more than 10 consecutive days.

Too slow for our particular business need.

Fortunately UnsafeAuthenticatedConnectionSharing came to the rescue.

proxy.OnGetWebRequest += (sender, e) =>
e.WebRequest.ConnectionGroupName = InitUserId.ToString();
e.WebRequest.UnsafeAuthenticatedConnectionSharing = true;

On the CrmService proxy you set the UnsafeAuthenticatedConnectionSharing to true. This just means that other calls to the web service will use the same connection to the database, and without reauthenticating. Which means that the next call with UnsafeAuthenticatedConnectionSharing set to true, will use the same connection. And will be assumed to be the same user. ConnectionGroupName is a convenient way to split up the connections so that we don’t accidentally reuse connections between different users. This can be done simply by using the guid of the current CRM User.

And now the important thing. This makes the web service call take 3/100 seconds. About 50 times faster ! The 600.000 calls can be done in 5 hours. Of course this speed was only attained because all calls where done internally on the CRM Server.

So why are these two settings not the default ? I don’t know. Perhaps because if you have a site with millions of users the amount of memory used up by each connection will be too big. But on a typical CRM site I would guess there would not be enough users to make this a problem.

But to be safe, if you just use the 2 properties in the areas that need performance with little concurrency between users you will be OK.

This and other performance best practices for CRM can be seen on technet. But I don’t think any will outperform this one.

Sunday, 07 March 2010 14:39:19 (GMT Standard Time, UTC+00:00)  #    Comments [0] -

# Friday, 13 November 2009

In the Day View project I needed to open a Windows 7 Sticky Note programmatically and write to it. Unfortunately I found out that there is not an API for the Sticky Notes application. At least not a managed one.

So I had to fake it and make my own API. I have made a small class that basicly uses Process.Start to open the program, and SendKeys to write to the Note.

Here it is:

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace DayView
public class StickyNote
private const string m_ProcessName = "StikyNot";
private readonly string m_ProcessFileName = Path.Combine(Environment.SystemDirectory, "StikyNot.exe");
private event EventHandler m_Activated = delegate { };
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

public void Activate()
bool makeNewNote = true;
Process p = FindProcess();
if (p == null)
p = StartProcess();
if (!NoteContainsText(p.MainWindowHandle))
makeNewNote = false;
var state = new StickyNoteState();
state.MakeNewNote = makeNewNote;
state.StickyNoteProcess = p;
ThreadPool.QueueUserWorkItem(Activate, state);

private void Activate(object state)
var stickyNoteState = state as StickyNoteState;
if (stickyNoteState.MakeNewNote)

private Process StartProcess()
var startInfo = new ProcessStartInfo(m_ProcessFileName);
Process p = Process.Start(startInfo);
Thread.Sleep(200); //This is an annoying hack. I haven't been able to find another way to be sure the process is started.
return p;

private void NewNote(Process p)

/// <summary>
/// Weird hack to find out if note contains text.
/// </summary>
/// <returns></returns>
private bool NoteContainsText(IntPtr handle)
string textOfClipboard = Clipboard.GetText();
string noteText = Clipboard.GetText().Trim();
if (textOfClipboard == null)
return !string.IsNullOrEmpty(noteText);

private Process FindProcess()
Process[] processes = Process.GetProcessesByName(m_ProcessName);
if(processes != null && processes.Length > 0)
return processes[0];
return null;

internal void OnActivated()
m_Activated(this, new EventArgs());

public event EventHandler Activated
add { m_Activated += value; }
remove { m_Activated -= value; }

public void Signal(string message)

public class StickyNoteState
public bool MakeNewNote { get; set; }
public Process StickyNoteProcess { get; set; }


It works OK. The only really buggy thing in the code is in line 47, where I have to use a Thread.Sleep in order to make sure the note is loaded before I write to it. Unfortunately this is necessary because the Process class does not provide an event to signal when it has finished loading. I arbitrarily chose 200 milliseconds for the Sleep. Larger values might be necessary on slower computers.

In order to use the class you have to configure SendKeys in your app.config like this:

<?xml version="1.0" encoding="utf-8" ?>
<add key="SendKeys" value="SendInput"/>
Friday, 13 November 2009 23:28:10 (GMT Standard Time, UTC+00:00)  #    Comments [4] -
.NET | Tips & Tricks | Windows 7

I am currently developing the game Klimakonflikt with 5 other guys. It is a Retro arcade game, Pac-man style, with nice 2D Graphics and music. Here is a screenshot from the game:


The current release, documentation and source code can be downloaded from here 

The game is inherently for 2 players but we are working on the single player edition. The AI in the current release is pretty daft, but in the next release it will be much improved. Also we are working on a WPF leveleditor, powerups and other fun stuff.

One of my co-developers Jakob has released a blog about the game and XNA development in general at

Friday, 13 November 2009 23:11:29 (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Games & Puzzles | WPF | XNA
# Tuesday, 20 October 2009

Last friday (the 9th) just one day before the deadline I finished my application for the Windows 7 Contest. "DayView" here is the description I made for the contest:

"DayView provides an overview of the appointments in all your calendars by a clever use of the Windows 7 Task bar. It shows your appointments, and you can scroll through them with toolbar buttons, and create Sticky Notes for the most important appointments. DayView uses MEF for a plugable architecture. It comes with plugins for Outlook and Sunbird."

Now I just have to wait for the evaluation the 26th of october. I have not yet decided whether I will continue working on DayView or move on to other stuff.

Tuesday, 20 October 2009 22:11:16 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -

# Monday, 03 August 2009

I must admit that I have never understood why it was fun to solve Sudoku's. But as a programmer I was fascinated with the idea of making the perfect Sudoku puzzle generator algorithm from the very first time I heard of Sudoku. Of course I wanted to make it so general that it could not only make the boring old 9x9 sudoku. But sodukus of any size 4x4, 9x9, 16x16, 25x25 and so on...

This resulted in a .NET 1.1 WinForms program 4-5 years ago in which I could randomly generate all sudoku combinations from 4x4 to 36x36. I created an algorithm that started in the upper left corner and put in a random number then moving to the next square put in a new random number from the remaining numbers. This meant that the algorithm had to backtrack when coming to a deadend where no number was valid. This approach guaranteed that all possible Sudokus could be generated, but it was extremely slow. Well for 9x9 sudokus it was less than a second but for 36x36 sudokus it was around 16 hours :O)

A few weeks ago I thought it was time to try out Silverlight and make a small program to test Microsofts "new" programming platform, but I had to come up with an idea for a small program. And for some reason Sudoku popped up again.

And I came up with a novel idea for an extremely fast generator (try it here). It has been said that no ideas are new anymore and I am pretty sure that I am not the first to think of this since the algorithm is pretty obvious. I start with a valid Sudoku as a template and then apply transformations of the columns, the rows and the numbers themselves.

To generate a valid Sudoku I first generate the template. The template is generated by starting with 1 in the upper left corner and then counting upwards. Next I go to the 4th row and start in the second column counting from 1 again like this:


Next I go to the 7th row and the 3rd columm and so on. So I add 3 rows (the square root of 9) each time. In a 16x16 sudoku I add 4 rows each time. The final template is generated in a few milliseconds even in a 400x400 Sudoku. The final template for a 9x9 sudoku looks like this:


The template is itself a valid Sudoku.

Now I make the transformation using an observation I have made. In any valid Sudoku I can make another valid Sudoku by switching the columns within each block of 3 columns. As indicated in red colour for the first block above.

Secondly I can do the same for the rows.

And last I can transform the numbers themselves so that all 1's are transformed to a number from 1 to 9. all 2's are transformed to one of the remaining 8 numbers and so on.

I could also have applied a 4th transformation  that switched the big blocks as columns and rows, but I don't think that it will add much value.

In all this the entire puzzle can be generated in milliseconds for even extremely big Sudokus.

Try it out yourself at

A few calculations

Now for some thoughts on completeness.

A 4x4 Sudoku has 4x3x2x3x2x2 = 288 possible combinations. Not counting combinations for empty tiles. In my original algorithm I was sure I covered all these combinations.

When using my new algorithm I can generate 2x2x2x2x4x3x2 = 384 possible transformations for a 4x4 Sudoku. This is more than the number of possible Sudokus. Which means that some of these transformations must result in the same Sudokus. And furthermore I do not know if there are Sudokus not covered by the transformations.

In a 9x9 Sudoku there are 1,83493E+21 possible Sudokus and I can generate 264.539.520 of those or 2.380.855.680 if I had used the extra big block transformations. Not complete at all, but on the other hand a very large amount of Sudokus and at lightning speed.

Empty tiles

A Sudoku isn't really a Sudoku without the empty tiles left for the person to fill out.

I just generate the empty tiles by choosing them at random. This method could probably lead to either very boring Sudokus or Sudokus that are impossible to solve. One day I might device some clever algorithm for emptying tiles in a more interesting fashion :O)


My conlusions on this my very first silverlight project is that it is extremely easy to make something appealing in silverlight. On the other hand there are som downsides. It is difficult to communicate between different silverlight hosts. I can only pass strings. When you hit the print button I open another browser window. And I actually have to serialize the Sudoku Grid and deserialize it again in the print window.

Another downside is that is not really browser agnostic. I have tried with 3 browsers IE8, IE7 and Firefox 3.5. Right now the only one working properly is IE8. In IE7 the new browser I open for printing has no menu, and so you cannot really print :O) In Firefox the silverlighthost cannot be resized, which is apparently a known issue. But it is a problem in the Sudoku generator because I offer Sudokus of any size. This means that in Firefox only 4x4 and 9x9 Sudokus will look good.

BTW: The rendering of a very large Sudoku takes some time of course even though the generation is over in milliseconds.

For my next silverlight project I think I will try out Prism and also the Out-of-browser experience.

Monday, 03 August 2009 22:16:48 (GMT Daylight Time, UTC+01:00)  #    Comments [5] -
Games & Puzzles
# Wednesday, 15 July 2009

Just released version 1.2 of SolZip my convenient tool for Zipping Visual Studio 2008 solutions

The major is a single install for SolZipMME, so you won't have to install ManagedMenuExtensions beforehand.

The release notes are here:

  1. Bug Fix: when multiple projects where referering to the same file it was added to the archive multiple times.
  2. SolZipMME uses SaveFileDialog instead of FolderBrowserDialog for Zip files.
  3. A single file installer for SolZipMME.
  4. Clipboard functionality now works on Vista / Windows Server 2008.
  5. SolZipGuidance changed. The menu will no longer appear on projects that are not C# projects. SolZipMME not changed.
  6. Support for $(SolutionDir) and $(ProjectDir) placeholders added
  7. Now works on Windows Vista even if UAC is not turned off


Wednesday, 15 July 2009 16:05:27 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Tools | Visual Studio
# Monday, 29 June 2009

I just released version 1.1 of SolZip which can be downloaded from the download page at the SolZip site.

The new action packed version contains one major improvement, one bug fix, and a few name changes:

  1. Now Source Control bindings for TFS and SCC can be removed from sln and csproj files. The default for all 3 UI's is that the bindings are removed, but you can optionally choose not to remove them. Feel free to contact me if you need bindings from other source control vendors removed.
  2. A bug fixed that made WinZip clients give a warning when unzipping certain archives made by SolZip. The warning was given when there was .. (2 dots) in a path in one of the zipped files.
  3. The Name SunZip.exe for the commandline tool was changed to SolZip.exe. Because SunZip sounded too much like UnZip.
  4. The Name SolutionZipper changed to SolZip everywhere. Except for SolutionZipper.dll which has been changed to SolZipBasis.dll

In the next version I will try to include ManagedMenuExtensions in the install so you won't have to run more than one setup to install SolZipMME.

Enjoy the new version :O)

Monday, 29 June 2009 20:44:48 (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Tools | Visual Studio
# Thursday, 18 June 2009

Here is my take on the Best practice Dispose pattern for most situations. It is heavily influenced by Juval Löwy from IDesigns book "Programming .NET Components", and some other sources which I no longer remember, since I made it a few years ago and have stuck to it ever since. Here it is:

public class Class1 : IDisposable
private bool m_IsDisposed;


public void Dispose()

private void Dispose(bool isDisposing)
if (m_IsDisposed)

if (isDisposing)
m_IsDisposed = true;

protected virtual void FreeManagedRessources()
//Free managed ressources here. Typically by calling Dispose on them

protected virtual void FreeUnmanagedRessources()
//Free unmanaged ressources here.

Another thing is that you can inherit the class and override the FreeManagedRessources and FreeUnmanagedRessources as needed.

Thursday, 18 June 2009 21:38:51 (GMT Daylight Time, UTC+01:00)  #    Comments [6] -
<2010 June>
About the author/Disclaimer
I am a software architect with focus on Microsoft Technologies. I have been working with these in different large companies since 1995. I am currently employed at UVdata A/S.
Here is my View Jesper Niedermann's profile on LinkedIn

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Privacy policy
The privacy policy of this site.

© Copyright 2017
Jesper Niedermann
Sign In
Total Posts: 28
This Year: 0
This Month: 0
This Week: 0
Comments: 32
All Content © 2017, Jesper Niedermann
DasBlog theme 'Niedermann' created by Jesper Niedermann, based on 'Business' created by Christoph De Baene (delarou)