public MainForm() { InitializeComponent(); m_uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); var newAtom = Win32A.GlobalAddAtom("RTSS_time_reader"); if (newAtom != 0) { m_globalHotkeyAtom = newAtom; } m_flushTimerPeriod = new TimeSpan(0, 0, 1); m_flushFileTimer = new System.Timers.Timer(); m_flushFileTimer.AutoReset = true; m_flushFileTimer.Interval = m_flushTimerPeriod.TotalMilliseconds; m_flushFileTimer.Elapsed += OnFlushFileTimerElapsed; m_stopWritingTimer = new System.Timers.Timer(); m_stopWritingTimer.AutoReset = false; m_stopWritingTimer.Elapsed += StopWritingFileOnTimer; txtFolder.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); m_pipeReader = new PipeReader(); m_pipeReader.StateChanged += (p_sender, p_args) => UpdateStatus(); }
protected override void OnShown(EventArgs e) { base.OnShown(e); var hotkey = new Hotkey(Win32A.KeyModifiers.Alt | Win32A.KeyModifiers.Ctrl, Keys.NumLock); var registered = RegisterHotkey(m_globalHotkeyAtom.Value, hotkey); if (registered) { RegistredHotkey = hotkey; } else { txtHotkeyEditor.Text = Win32A.KeyModifiers.None.ToString(); if (m_globalHotkeyAtom.HasValue) { Win32A.GlobalDeleteAtom(m_globalHotkeyAtom.Value); m_globalHotkeyAtom = null; } } UpdateStatus(); StartListening(); }
private void txtHotkeyEditor_Enter(object sender, EventArgs e) { var dialog = new HotkeyEditorDialog(); dialog.RegistredHotkey = RegistredHotkey; dialog.HotkeyProcessor = this; if (dialog.ShowDialog(this) == DialogResult.OK) { if (m_globalHotkeyAtom.HasValue) { Win32A.UnregisterHotKey(this.Handle, m_globalHotkeyAtom.Value); var win32Error = Marshal.GetLastWin32Error(); if (win32Error != Win32A.ERROR_SUCCESS) { var ex = new Win32Exception(); MessageBox.Show(this, ex.Message, "Cannot regitster hotkey", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } RegistredHotkey = dialog.NewHotkey; m_globalHotkeyAtom = dialog.NewHotkeyAtom; } this.ActiveControl = null; }
public void UnregisterHotkey(ushort?p_atom) { if (p_atom.HasValue) { var atom = p_atom.Value; Win32A.UnregisterHotKey(this.Handle, atom); Win32A.GlobalDeleteAtom(atom); } }
private bool CreatePipe() { var result = false; try { string fullPath = PipeFullName; var handle = Win32A.CreateNamedPipe(fullPath , (uint)Win32A.PipeOpenModeFlags.PIPE_ACCESS_DUPLEX , (uint) (Win32A.PipeModeFlags.PIPE_TYPE_MESSAGE | Win32A.PipeModeFlags.PIPE_READMODE_MESSAGE | Win32A.PipeModeFlags.PIPE_WAIT) , 1 , 1024, 1024 , 0, IntPtr.Zero); if (handle.ToInt32() == -1) { var win32Error = Marshal.GetLastWin32Error(); if (win32Error != Win32A.ERROR_SUCCESS) { throw new Win32Exception(win32Error); } } var safePipeHandle = new SafePipeHandle(handle, true); m_pipeStream = new NamedPipeServerStream(PipeDirection.InOut, false, false, safePipeHandle); SetFlag(PipeReaderState.PipeCreated, true); m_lastException = null; result = true; WaitForConnection(); } catch (TimeoutException exception) { m_lastException = exception; SetFlag(PipeReaderState.Error, true); } catch (System.OperationCanceledException) { } catch (Exception exception) { m_lastException = exception; ClosePipe(); SetFlag(PipeReaderState.Error, true); } return(result); }
private void btnOk_Click(object sender, EventArgs e) { NewHotkeyAtom = Win32A.GlobalAddAtom("RTSS_time_reader" + GetHashCode().ToString()); var registred = HotkeyProcessor.RegisterHotkey(NewHotkeyAtom, NewHotkey); if (false == registred) { DialogResult = DialogResult.None; } else { DialogResult = DialogResult.OK; } }
public void DropConnection() { if (false == IsConnectionAccepted) { throw new InvalidOperationException("Invalid object state"); } m_stopReadWriteLoops = true; lock (m_threadHandleLocker) { if (ReadFlag(PipeReaderState.PipeIO) && m_taskThreadHandle != Win32A.INVALID_HANDLE_PTR) { Win32A.CancelSynchronousIo(m_taskThreadHandle); } } }
public bool RegisterHotkey(ushort p_atom, Hotkey p_hotkey) { var result = Win32A.RegisterHotKey(this.Handle, p_atom, p_hotkey.Modifiers, (int)p_hotkey.Key); if (false == result) { var win32Error = Marshal.GetLastWin32Error(); if (win32Error != Win32A.ERROR_SUCCESS) { var ex = new Win32Exception(); MessageBox.Show(this, ex.Message, "Cannot regitster hotkey", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } return(result); }
protected void ClosePipe() { ContinueAcceptingConnections = false; lock (m_threadHandleLocker) { if (ReadFlag(PipeReaderState.PipeIO) && m_taskThreadHandle != Win32A.INVALID_HANDLE_PTR) { Win32A.CancelSynchronousIo(m_taskThreadHandle); } } lock (m_pipeStreamLocker) { if (m_pipeStream != null) { m_pipeStream.Dispose(); m_pipeStream = null; } } SetFlag(PipeReaderState.ConnectionAccepted, false); SetFlag(PipeReaderState.PipeCreated, false); }
private void WaitForConnection() { SetFlag(PipeReaderState.PipeIO, true); try { m_pipeStream.WaitForConnection(); uint processId; if (Win32A.GetNamedPipeClientProcessId(m_pipeStream.SafePipeHandle.DangerousGetHandle(), out processId)) { ClientProcessId = processId; using (var process = Process.GetProcessById((int)ClientProcessId)) { ProcessName = process.ProcessName; } } } finally { SetFlag(PipeReaderState.PipeIO, false); } SetFlag(PipeReaderState.ConnectionAccepted, true); }
public void StartAcceptingConnections() { SetFlag(PipeReaderState.Started, true); ClientProcessId = Win32A.INVALID_HANDLE_VALUE; var previousTask = m_task; m_stopReadWriteLoops = false; m_task = Task.Factory.StartNew( () => { lock (m_threadHandleLocker) { var threadId = Win32A.GetCurrentThreadId(); m_taskThreadHandle = Win32A.OpenThread(0x40000000, false, threadId); } try { if (previousTask != null) { try { previousTask.Wait(); } catch (Exception) { ; //do nothing } } if (false == CreatePipe()) { return; } if (false == OpenFile()) { ClosePipe(); return; } SetFlag(PipeReaderState.FileOpened, true); MainLoop(); } finally { lock (m_threadHandleLocker) { if (m_taskThreadHandle != Win32A.INVALID_HANDLE_PTR) { Win32A.CloseHandle(m_taskThreadHandle); } m_taskThreadHandle = Win32A.INVALID_HANDLE_PTR; m_task = null; } State = PipeReaderState.None; } } , CancellationToken.None , TaskCreationOptions.LongRunning , TaskScheduler.Default ); }