/* Sharememory_UINT32 Address: 0: The average of time span of twice calllback function called by periodically upon expiration of real-time timer1 (unit:milliseconds) 1: The minimum of time span of twice calllback function called by periodically upon expiration of real-time timer1 (unit:milliseconds) 2: The maximum of time span of twice calllback function called by periodically upon expiration of real-time timer1 (unit:milliseconds) 10: Records the starting time difference of 2 program (RTTPrj.exe and RTTimerP.exe) */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Threading; using HANDLE = System.IntPtr; namespace RTTPrj { public partial class Form1 : Form { [DllImport("coredll.dll")] public static extern IntPtr FindWindow(string lpWindowName, string lpClassName); [DllImport("coredll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)] public static extern HANDLE CreateEvent(HANDLE lpEventAttributes, [In, MarshalAs(UnmanagedType.Bool)] bool bManualReset, [In, MarshalAs(UnmanagedType.Bool)] bool bIntialState, [In, MarshalAs(UnmanagedType.BStr)] string lpName); [DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CloseHandle(HANDLE hObject); [DllImport("coredll.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EventModify(HANDLE hEvent, [In, MarshalAs(UnmanagedType.U4)] int dEvent); [DllImport("coredll.Dll", EntryPoint = "CreateProcess", SetLastError = true)] extern static int CreateProcess(string strImageName, string strCmdLine, IntPtr pProcessAttributes, IntPtr pThreadAttributes, int bInheritsHandle, int dwCreationFlags, IntPtr pEnvironment, IntPtr pCurrentDir, IntPtr bArray, ProcessInfo oProc); [DllImport("coredll.dll")] static extern uint GetTickCount(); private const string shareMemorylib = "RTTimerDll.dll"; // To Read/Write share memory boolean data [DllImport(shareMemorylib)] private static extern bool RTT_ReadBOOL(Int32 memslot, ref bool rbool32Array); [DllImport(shareMemorylib)] private static extern bool RTT_WriteBOOL(Int32 memslot, bool bool32Array); // To Read/Write share memory float data [DllImport(shareMemorylib)] private static extern bool RTT_ReadFLOAT(Int32 memslot, ref float rfloatValue); [DllImport(shareMemorylib)] private static extern bool RTT_WriteFLOAT(Int32 memslot, float floatValue); // To Read/Write share memory Int32 data [DllImport(shareMemorylib)] private static extern bool RTT_ReadDWORD(Int32 memslot, ref UInt32 ruint32Value); [DllImport(shareMemorylib)] private static extern bool RTT_WriteDWORD(Int32 memslot, UInt32 uint32Value); public class ProcessInfo { public Int32 hProcess; public Int32 hThread; public Int32 ProcessID; public Int32 ThreadID; } public enum EventFlags { PULSE = 1, RESET = 2, SET = 3 } private static bool SetEvent(HANDLE hEvent) { return EventModify(hEvent, (int)EventFlags.SET); } private static bool ResetEvent(HANDLE hEvent) { return EventModify(hEvent, (int)EventFlags.RESET); } ThreadStart worker; Thread t; UInt32 RTTPrjStartTick = 0; // IntPtr hPort; bool bRunThread = false; delegate void ShowTextCallback(string inputstr); delegate void ShowOnTextCallback(ref TextBox rtb, string inpstr); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { worker = new ThreadStart(IO_work); t = new Thread(worker); //t.IsBackground = true; ProcessInfo pi = new ProcessInfo(); string sparameter = textBox1.Text; //Timer interval RTTPrjStartTick = GetTickCount(); CreateProcess("\\temp\\RTTimerP.exe", sparameter, IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, pi); Thread.Sleep(5); t.Start(); bRunThread = true; } private void button2_Click(object sender, EventArgs e) { bRunThread = false; Thread.Sleep(10); t.Abort(); t.Join(); IntPtr p = CreateEvent(HANDLE.Zero, true, false, "CLOSERTTIMER"); SetEvent(p); ResetEvent(p); CloseHandle(p); IntPtr Hwnd = FindWindow(null, "RTTimerP"); if (Hwnd != IntPtr.Zero) { //0x0010 WM_CLOSE Console.WriteLine("send WM_close"); SendMessage(Hwnd, 0x0010, IntPtr.Zero, IntPtr.Zero); } } private void IO_work() { UInt32 uiValue = 0; uint IDValue = 0; bool iRet; string errMessage = new string(' ', 128); while (bRunThread==true) { //try //{ RTT_ReadDWORD(10, ref uiValue); //read DWORD data form share memory slot 1 if(uiValue>0) ShowOnTextBox(ref this.textBox6, Convert.ToString(uiValue - RTTPrjStartTick)); /*Read DWORD share memory*/ iRet = RTT_ReadDWORD(0, ref IDValue); if (iRet) { ShowOnTextBox(ref this.txtAvgValue, Convert.ToString(IDValue)); } iRet = RTT_ReadDWORD(1, ref IDValue); if (iRet) { ShowOnTextBox(ref this.txtMinValue, Convert.ToString(IDValue)); } iRet = RTT_ReadDWORD(2, ref IDValue); if (iRet) { ShowOnTextBox(ref this.txtMaxValue, Convert.ToString(IDValue)); } Thread.Sleep(5); /*} catch (Exception ex) { Console.WriteLine(ex); }*/ } } private void ShowOnTextBox(ref TextBox rtb,string inpstr) { if(rtb.InvokeRequired) { ShowOnTextCallback d = new ShowOnTextCallback(ShowOnTextBox); this.Invoke(d, new object[] { rtb,inpstr }); } else { rtb.Text = inpstr; } } } }