Roemer

Sometimes you might want to listen for a Process Start and do some Actions.

To start listening, just run the following code:

public void StartProcessMonitoring()
{
	// Create event query to be notified within 1 second of a change in a service
	WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");

	// Create the Watcher
	watcher = new ManagementEventWatcher();
	watcher.Query = query;
	watcher.EventArrived += new EventArrivedEventHandler(ProcessStartEvent);
	watcher.Start();
}

The callback could then look like this:

public static void ProcessStartEvent(object sender, EventArrivedEventArgs e)
{
	ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
	string processName = targetInstance.Properties["Name"].Value.ToString();
	if (processName == "myprocess.exe")
	{
		// Do whatever you want
	}
}

I created a member for the watcher Object

ManagementEventWatcher watcher;

and closed it when the App closes, otherwise I got a COM Error or something.
Here’s the cleanup Code:

watcher.Stop();
watcher.Dispose();

I hate that Home Screen which pops up on every Skype start. I really do…
So I created a small Tray Icon app which looks for this Window and closes it immediately.

As usual, there is some Win32 API needed to do this. Here’s the Code which searches and closes the Window:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public const Int32 WM_SYSCOMMAND = 0x0112;
public const Int32 SC_CLOSE = 0xF060;

public static void Close()
{
	IntPtr hWnd = FindWindow("THomeForm", "Skype Home");
	if (hWnd.ToInt32() > 0)
	{
		SendMessage(hWnd, WM_SYSCOMMAND, new IntPtr(SC_CLOSE), IntPtr.Zero);
	}
}

Here’s the programm: SkypeHomeCloser_1.0

Code printed in Visual Studio 2010 is not colored.
To enable this “feature”, you have to install this Extension.
Be aware that you can’t collapse regions for printing with this Extension.
Furthermore, if you wan’t line numbers, you have to enable them via Tools->Options.

I needed to grab a paged list of Data from some Website and wanted to do it with a Browser Automation Tool.

There’s Telerik’s Free Testing Framework but that one needs to be installed (because it uses Plugins/Addons for the Browser) in order to run.

I found a very nice alternative: WatiN (or http://sourceforge.net/projects/watin if the other page is down)
It’s a simple Library which allows everything needed when automating webbrowsers.

The Microsoft SQL Server Management Studio (even v2008 R2) has problems with executing large SQL-files. To execute these files you need to use the sqlcmd Utility.

Sample usage:

sqlcmd -S myServer\instanceName -i -U loginname -P password C:\myScript.sql

I always forget how to Rollback a Changeset, so here’s the command to do it:

First, make sure you got the Team Foundation Server Power Tools installed.

Then execute the following command:

tfpt rollback /changeset:<changesetnumber> "<workspace>"

so for example:

tfpt rollback /changeset:48745 "C:\Workspaces\XXXComplete"

If you need to debug in special condition, like you got a loop and you want to debug the 34th item, you could either set a breakpoint and just skip it 33 times (by pressing f5) or you could simply add a condition to this breakpoint.
You do this by right-clicking the breakpoint and choosing "Condition...".
There you can enter any valid C#-Expression, like "ssList[counter] == 1947635". Now the breakpoint is only hit if this condition is true.