/// <summary> /// Stopped listening to system events. /// </summary> /// <remarks> /// Listen Form is showed by sub thread, It could not be directly closed by main thread. /// </remarks> public void Stop() { try { log.Info("Stopping listening thread"); MethodInvoker invoker = new MethodInvoker( delegate() { //Anonymous method for closing form. listenForm.Close(); //Form thread will not automatically execute Dispose after close the form. //If have not executed Dispose here, main thread will encounter an dispose error when exit application. listenForm.Dispose(); listenForm = null; } ); listenForm.Invoke(invoker); log.Debug("Listening form closed"); if (listenThread != null && listenThread.IsAlive) { listenThread.Join(); } listenThread = null; IsWorking = false; } catch (System.Exception ex) { log.Error("There are some errors when stop listening!", ex); } }
/// <summary> /// Start Listening to system events /// </summary> /// <remarks> /// Many system message API request a form. So a background model form will be loaded by a sub thread. /// </remarks> public void Start() { log.Info("Starting listening thread"); //ThreadStart entry = new ThreadStart(StartEntry); listenThread = new Thread( delegate() { listenForm = new MessageListenForm(this); listenForm.ShowDialog(); } ); listenThread.Start(); IsWorking = true; }