Directly sending keystrokes to another process via hooking

This is a little code that allows you to send message to a backgrounded application. To send the “A” char for example, simply call sendKeystroke(Keys.A), and don’t forget to use namespace System.windows.forms to be able to use the Keys object. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace keybound { … Read more

ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew

If you’re going to start a long-running task with TPL, you should specify TaskCreationOptions.LongRunning, which will mean it doesn’t schedule it on the thread-pool. (EDIT: As noted in comments, this is a scheduler-specific decision, and isn’t a hard and fast guarantee, but I’d hope that any sensible production scheduler would avoid scheduling long-running tasks on … Read more

How do I check if a property exists on a dynamic anonymous type in c#?

public static bool DoesPropertyExist(dynamic settings, string name) { if (settings is ExpandoObject) return ((IDictionary<string, object>)settings).ContainsKey(name); return settings.GetType().GetProperty(name) != null; } var settings = new {Filename = @”c:\temp\q.txt”}; Console.WriteLine(DoesPropertyExist(settings, “Filename”)); Console.WriteLine(DoesPropertyExist(settings, “Size”)); Output: True False

Why is AddRange faster than using a foreach loop?

Potentially, AddRange can check where the value passed to it implements IList or IList<T>. If it does, it can find out how many values are in the range, and thus how much space it needs to allocate… whereas the foreach loop may need to reallocate several times. Additionally, even after allocation, List<T> can use IList<T>.CopyTo … Read more

How do I create an COM visible class in C#?

OK I found the solution and I’ll write it here for the common good. Start VS2010 as administrator. Open a class library project (exmaple – MyProject). Add a new interface to the project (see example below). Add a using System.Runtime.InteropServices; to the file Add the attributes InterfaceType, Guid to the interface. You can generate a … Read more