예제 #1
0
        /// <summary>
        /// prepares Background Worker but does not start it yet.
        /// </summary>
        void PrepareBackgroundWorker()
        {
            // get our dispatcher
            System.Windows.Threading.Dispatcher dispatcher = this.Dispatcher;

            // create our background worker and support cancellation
            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = true;

            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                Debug.WriteLine("IP: RunWorker started");

                try
                {
                    isWorkerRunning = true;
                    // open currentPort
                    lidarLiteProcessor = new LidarLiteProcessor();
                    lidarLiteProcessor.Open(new string[] { currentPort });
                    lidarLiteProcessor.DataReceivedEvent += lidarLiteProcessor_DataReceived;

                    Dispatcher.Invoke(new Action<object>(EnableOpenCloseButton), "");

                    while (true)
                    {
                        lidarLiteProcessor.StartedLoop();   // helps keeping 20ms cycle steady

                        if (worker.CancellationPending)
                        {
                            Debug.WriteLine("IP: RunWorker Cancellation Pending, closing serial port");

                            lidarLiteProcessor.Close();

                            args.Cancel = true;

                            Debug.WriteLine("OK: RunWorker Cancellation sequence completed");

                            isWorkerRunning = false;

                            return;
                        }

                        lidarLiteProcessor.Process();

                        DisplayAll();

                        lidarLiteProcessor.WaitInLoop();  // we won't wait here longer than lidarLiteProcessor.desiredLoopTimeMs - about 20ms-<already elapsed time>
                    }
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("Error: RunWorker: " + exc);

                    // create a new delegate for updating our status text
                    UpdateStatusDelegate update = new UpdateStatusDelegate(UpdateStatusText);

                    // invoke the dispatcher and pass the error data:
                    Dispatcher.BeginInvoke(update, "Error: RunWorker: " + exc.Message);

                    lidarLiteProcessor.Close();     // close communication to the serial port

                    Dispatcher.Invoke(new Action<object>(ResetOpenCloseButton), "");
                }
            };

            worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                isWorkerRunning = false;

                Dispatcher.Invoke(new Action<object>(EnableOpenCloseButton), "");

                Debug.WriteLine("OK: RunWorker Completed");
            };
        }
예제 #2
0
        /// <summary>
        /// prepares Background Worker but does not start it yet.
        /// </summary>
        void PrepareBackgroundWorker()
        {
            // get our dispatcher
            System.Windows.Threading.Dispatcher dispatcher = this.Dispatcher;

            // create our background worker and support cancellation
            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = true;

            worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                Debug.WriteLine("IP: RunWorker started");

                try
                {
                    isWorkerRunning = true;
                    // open currentPort
                    lidarLiteProcessor = new LidarLiteProcessor();
                    lidarLiteProcessor.Open(new string[] { currentPort });
                    lidarLiteProcessor.DataReceivedEvent += lidarLiteProcessor_DataReceived;

                    Dispatcher.Invoke(new Action <object>(EnableOpenCloseButton), "");

                    while (true)
                    {
                        lidarLiteProcessor.StartedLoop();   // helps keeping 20ms cycle steady

                        if (worker.CancellationPending)
                        {
                            Debug.WriteLine("IP: RunWorker Cancellation Pending, closing serial port");

                            lidarLiteProcessor.Close();

                            args.Cancel = true;

                            Debug.WriteLine("OK: RunWorker Cancellation sequence completed");

                            isWorkerRunning = false;

                            return;
                        }

                        lidarLiteProcessor.Process();

                        DisplayAll();

                        lidarLiteProcessor.WaitInLoop();  // we won't wait here longer than lidarLiteProcessor.desiredLoopTimeMs - about 20ms-<already elapsed time>
                    }
                }
                catch (Exception exc)
                {
                    Debug.WriteLine("Error: RunWorker: " + exc);

                    // create a new delegate for updating our status text
                    UpdateStatusDelegate update = new UpdateStatusDelegate(UpdateStatusText);

                    // invoke the dispatcher and pass the error data:
                    Dispatcher.BeginInvoke(update, "Error: RunWorker: " + exc.Message);

                    lidarLiteProcessor.Close();     // close communication to the serial port

                    Dispatcher.Invoke(new Action <object>(ResetOpenCloseButton), "");
                }
            };

            worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                isWorkerRunning = false;

                Dispatcher.Invoke(new Action <object>(EnableOpenCloseButton), "");

                Debug.WriteLine("OK: RunWorker Completed");
            };
        }