C# links

Here follows a collection of useful C# links/bookmarks.

We hope that they are useful for you.

 

 

 

 

 

 

Update GUI from worker thread

Overview:

  1. http://stackoverflow.com/questions/2097284/update-ui-from-multiple-worker-threads-net

The code below is excellent for Windows Forms and needs just a little modification for WPF!! Taken from: http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c:

using System;
using System.Windows.Forms;
using System.Threading;

namespace Test

{
public partial class UIThread : Form
{
Worker worker;
Thread workerThread;
public UIThread()
{
InitializeComponent();
worker = new Worker();
worker.ProgressChanged += new EventHandler<ProgressChangedArgs>(OnWorkerProgressChanged);
workerThread = new Thread(new ThreadStart(worker.StartWork));
workerThread.Start();
}

private void OnWorkerProgressChanged(object sender, ProgressChangedArgs e)
{
//cross thread – so you don’t get the cross theading exception
if (this.InvokeRequired)
{
this.BeginInvoke((MethodInvoker)delegate
{
OnWorkerProgressChanged(sender, e);
});
return;
}
//change control
this.label1.Text = e.Progress;
}
}

public class Worker
{

public event EventHandler<ProgressChangedArgs> ProgressChanged;

protected void OnProgressChanged(ProgressChangedArgs e)
{
if(ProgressChanged!=null)
{
ProgressChanged(this,e);
}
}

public void StartWork()
{
Thread.Sleep(100);
OnProgressChanged(new ProgressChangedArgs(“Progress Changed”));
Thread.Sleep(100);
}
}

public class ProgressChangedArgs : EventArgs
{
public string Progress {get;private set;}
public ProgressChangedArgs(string progress)
{
Progress = progress;
}
}
}

WPF Dispatcher.BeginInvoke: http://msdn.microsoft.com/en-us/library/ms591587.aspx

Thread safe GUI: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).asp

By using BackgroundWorker:

  1. http://stackoverflow.com/questions/2097284/update-ui-from-multiple-worker-threads-net
  2. http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
  3. http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

Threading

  1. Microsoft overview: http://msdn.microsoft.com/en-us/library/ms741870.aspx
  2. Thread synchronization: http://msdn.microsoft.com/en-us/library/ms173179.aspx
  3. Threading in C#: http://www.albahari.com/threading/
  4. events and thread safty: http://stackoverflow.com/questions/786383/c-sharp-events-and-thread-safety
  5. .Net 2003: http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
  6. http://www.knowdotnet.com/articles/responsiveui.html
  7. http://www.codeproject.com/Articles/1280/Worker-Threads-in-C

Invoke/BeginInvoke

Difference between “Invoke” and “BeginInvoke”:

  1. http://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke

Logging

  1. http://csharp-source.net/open-source/logging

C# tutorials

1. .Net 2003: http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx

Events and delegates

  1. .Net 2003: http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx

Interfaces

  1. http://www.daniweb.com/software-development/csharp/threads/114364
  2. http://fci-h.blogspot.com/2008/03/oop-design-concepts-interfaces_05.html

SendInput

http://www.ownedcore.com/forums/world-of-warcraft/world-of-warcraft-bots-programs/wow-bots-questions-requests/317470-c-sendinput-blocked.html

 

   public class Input
   {
      private static InputWrapper KeyboardWrapper(ushort scanCode, bool keyUp)
      {
         InputWrapper wrapper = new InputWrapper();
         wrapper.Type = SendInputType.Keyboard;
         wrapper.MKH.Keyboard = new KeyboardInputData()
         {
            Key = 0,
            Scan = scanCode,
            Flags = (uint)KeyboardFlags.ScanCode,
            Time = 0,
            ExtraInfo = IntPtr.Zero
         };
         if (keyUp)
            wrapper.MKH.Keyboard.Flags |= (uint)KeyboardFlags.KeyUp;
         return wrapper;
      }
      public static void SimulateKeyPress(ushort scanCode, bool shift, bool ctrl, bool alt)
      {
         List<InputWrapper> inputs = new List<InputWrapper>();
         // Add the key down and up
         inputs.Add(KeyboardWrapper(scanCode, false));
         inputs.Add(KeyboardWrapper(scanCode, true));
         // For each of the modifier keys, put a key down before and key up after the key we’re simulating
         if (shift)
         {
            inputs.Insert(0, KeyboardWrapper((ushort)DirectXScanCode.DIK_LSHIFT, false));
            inputs.Add(KeyboardWrapper((ushort)DirectXScanCode.DIK_LSHIFT, true));
         }
         if (ctrl)
         {
            inputs.Insert(0, KeyboardWrapper((ushort)DirectXScanCode.DIK_LCONTROL, false));
            inputs.Add(KeyboardWrapper((ushort)DirectXScanCode.DIK_LCONTROL, true));
         }
         if (alt)
         {
            inputs.Insert(0, KeyboardWrapper((ushort)DirectXScanCode.DIK_LMENU, false));
            inputs.Add(KeyboardWrapper((ushort)DirectXScanCode.DIK_LMENU, true));
         }
         // Send the list of inputs to the foreground application
         SendInput((uint)inputs.Count, inputs.ToArray(), Marshal.SizeOf(typeof(InputWrapper)));
      }
      [DllImport("user32.dll", SetLastError = true)]
      static extern uint SendInput(uint numberInputs, InputWrapper[] inputs, int sizeOfStructure);
      [StructLayout(LayoutKind.Sequential)]
      private struct InputWrapper
      {
         public SendInputType Type;
         public MouseKeyboardHardwareUnion MKH;
      }
      private enum SendInputType : int
      {
         Mouse = 0,
         Keyboard,
         Hardware
      }
      [StructLayout(LayoutKind.Explicit)]
      private struct MouseKeyboardHardwareUnion
      {
         [FieldOffset(0)]
         public MouseInputData Mouse;
         [FieldOffset(0)]
         public KeyboardInputData Keyboard;
         [FieldOffset(0)]
         public HardwareInputData Hardware;
      }
      [StructLayout(LayoutKind.Sequential)]
      private struct MouseInputData
      {
         public int X;
         public int Y;
         public uint MouseData;
         public MouseEventFlags Flags;
         public uint Time;
         public IntPtr ExtraInfo;
      }
      [StructLayout(LayoutKind.Sequential)]
      private struct KeyboardInputData
      {
         public ushort Key;
         public ushort Scan;
         public uint Flags;
         public uint Time;
         public IntPtr ExtraInfo;
      }
      [StructLayout(LayoutKind.Sequential)]
      private struct HardwareInputData
      {
         public int Msg;
         public short ParamL;
         public short ParamH;
      }
      [Flags]
      enum MouseDataFlags : uint
      {
         XButton1 = 0×0001,
         XButton2 = 0×0002
      }
      [Flags]
      enum KeyboardFlags : uint
      {
         ExtendedKey = 0×1,
         KeyUp = 0×2,
         Unicode = 0×4,
         ScanCode = 0×8
      }
      [Flags]
      enum MouseEventFlags : uint
      {
         Move = 0×0001,
         LeftDown = 0×0002,
         LeftUp = 0×0004,
         RightDown = 0×0008,
         RightUp = 0×0010,
         MiddleDown = 0×0020,
         MiddleUp = 0×0040,
         XDown = 0×0080,
         XUp = 0×0100,
         Wheel = 0×0800,
         VirtualDesktop = 0×4000,
         Absolute = 0×8000
      }
      public enum DirectXScanCode : ushort
      {
         DIK_ESCAPE          = 0×01,
         DIK_1               = 0×02,
         DIK_2               = 0×03,
         DIK_3               = 0×04,
         DIK_4               = 0×05,
         DIK_5               = 0×06,
         DIK_6               = 0×07,
         DIK_7               = 0×08,
         DIK_8               = 0×09,
         DIK_9               = 0x0A,
         DIK_0               = 0x0B,
         DIK_MINUS           = 0x0C,   /* – on main keyboard */
         DIK_EQUALS          = 0x0D,
         DIK_BACK            = 0x0E,   /* backspace */
         DIK_TAB             = 0x0F,
         DIK_Q               = 0×10,
         DIK_W               = 0×11,
         DIK_E               = 0×12,
         DIK_R               = 0×13,
         DIK_T               = 0×14,
         DIK_Y               = 0×15,
         DIK_U               = 0×16,
         DIK_I               = 0×17,
         DIK_O               = 0×18,
         DIK_P               = 0×19,
         DIK_LBRACKET        = 0x1A,
         DIK_RBRACKET        = 0x1B,
         DIK_RETURN          = 0x1C,   /* Enter on main keyboard */
         DIK_LCONTROL        = 0x1D,
         DIK_A               = 0x1E,
         DIK_S               = 0x1F,
         DIK_D               = 0×20,
         DIK_F               = 0×21,
         DIK_G               = 0×22,
         DIK_H               = 0×23,
         DIK_J               = 0×24,
         DIK_K               = 0×25,
         DIK_L               = 0×26,
         DIK_SEMICOLON       = 0×27,
         DIK_APOSTROPHE      = 0×28,
         DIK_GRAVE           = 0×29,   /* accent grave */
         DIK_LSHIFT          = 0x2A,
         DIK_BACKSLASH       = 0x2B,
         DIK_Z               = 0x2C,
         DIK_X               = 0x2D,
         DIK_C               = 0x2E,
         DIK_V               = 0x2F,
         DIK_B               = 0×30,
         DIK_N               = 0×31,
         DIK_M               = 0×32,
         DIK_COMMA           = 0×33,
         DIK_PERIOD          = 0×34,   /* . on main keyboard */
         DIK_SLASH           = 0×35,  /* / on main keyboard */
         DIK_RSHIFT          = 0×36,
         DIK_MULTIPLY        = 0×37,   /* * on numeric keypad */
         DIK_LMENU           = 0×38,  /* left Alt */
         DIK_SPACE           = 0×39,
         DIK_CAPITAL         = 0x3A,
         DIK_F1              = 0x3B,
         DIK_F2              = 0x3C,
         DIK_F3              = 0x3D,
         DIK_F4              = 0x3E,
         DIK_F5              = 0x3F,
         DIK_F6              = 0×40,
         DIK_F7              = 0×41,
         DIK_F8              = 0×42,
         DIK_F9              = 0×43,
         DIK_F10             = 0×44,
         DIK_NUMLOCK         = 0×45,
         DIK_SCROLL          = 0×46,   /* Scroll Lock */
         DIK_NUMPAD7         = 0×47,
         DIK_NUMPAD8         = 0×48,
         DIK_NUMPAD9         = 0×49,
         DIK_SUBTRACT        = 0x4A,   /* – on numeric keypad */
         DIK_NUMPAD4         = 0x4B,
         DIK_NUMPAD5         = 0x4C,
         DIK_NUMPAD6         = 0x4D,
         DIK_ADD             = 0x4E,   /* + on numeric keypad */
         DIK_NUMPAD1         = 0x4F,
         DIK_NUMPAD2         = 0×50,
         DIK_NUMPAD3         = 0×51,
         DIK_NUMPAD0         = 0×52,
         DIK_DECIMAL         = 0×53,   /* . on numeric keypad */
         DIK_F11             = 0×57,
         DIK_F12             = 0×58,
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         // You have 5 seconds to switch the application on which you’d like to receive the
         // event to the foreground
         Thread.Sleep(5000);
         Input.SimulateKeyPress((ushort)Input.DirectXScanCode.DIK_3, false, false, false);
      }
   }
public static void MouseMoveBy(int x, int y)
      {
         InputWrapper input = new InputWrapper();
         input.Type = SendInputType.Mouse;
         input.MKH.Mouse = new MouseInputData()
         {
            X = x,
            Y = y,
            Flags = MouseEventFlags.Move,
         };
         InputWrapper[] inputs = new InputWrapper[] { input };
         SendInput((uint)1, inputs, Marshal.SizeOf(typeof(InputWrapper)));
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>