예제 #1
0
 public override void Dispose(bool force)
 {
     base.Dispose(force);
     Unsetup();
     LoopCTS?.Dispose();
     LoopCTS = null;
     IsSynchronizable?.Dispose();
     IsSynchronizable = null;
     WaitForSync?.Dispose();
     WaitForSync = null;
 }
예제 #2
0
        private void SetupRunProgramTask()
        {
            LoopCTS.Dispose();
            LoopCTS = new CancellationTokenSource();
            try
            {
                LoopingTask?.Dispose();
            }
            catch (Exception ex)
            { }
            LoopingTask = new Task(() =>
            {
                try
                {
                    RunProgramThread = Thread.CurrentThread;
                    while (true)
                    {
                        //if sync is requested, go into synchronizable state
                        if (SyncContext != null)
                        {
                            lock (SyncStateRequestLock)
                            {
                                if (IsSyncStateRequested)
                                {
                                    DebugTools.AddEvent("LoopingZoneProgram.LoopingTask", "Entering Sync-State: " + Name);
                                    IsSynchronizable.Fire(this, null);
                                    DebugTools.AddEvent("LoopingZoneProgram.LoopingTask",
                                                        "In Sync-State - Waiting for Signal from SyncContext: " + Name);
                                    WaitForSync.WaitForFire();
                                    DebugTools.AddEvent("LoopingZoneProgram.LoopingTask", "Leaving Sync-State: " + Name);
                                    IsSyncStateRequested = false;
                                    DebugTools.AddEvent("LoopingZoneProgram.LoopingTask", "IsSyncStateRequested = false: " + Name);
                                }
                            }
                        }

                        //this is currently not doing anything
                        LeftSyncTrigger.Fire(this, null);

                        DebugTools.AddEvent("LoopingZoneProgram.LoopingTask", "Starting Loop: " + Name);
                        //start loop
                        Loop();
                        DebugTools.AddEvent("LoopingZoneProgram.LoopingTask", "Finished Loop: " + Name);

                        //if cancellation is requested, break out of loop after setting notification parameters for the consumer
                        if (LoopCTS.IsCancellationRequested)
                        {
                            Running = false;
                            StopTrigger.Fire(this, null);
                            break;
                        }

                        //this makes the CPU consumption way lesser
                        Thread.Sleep(LoopWaitTime);
                    }
                }
                catch (ThreadAbortException ex)
                {
                    //DebugTools.AddEvent("LoopingZoneProgram.LoopingTask.Method", "LoopingTask thread aborted");
                    //DebugTools.AddEvent("LoopingZoneProgram.Stop", "START Setting Running = false");
                    Running = false;
                    StopTrigger.Fire(this, null);
                    //DebugTools.AddEvent("LoopingZoneProgram.Stop", "END Setting Running = false");
                }
                catch (Exception ex)
                {
                    Running = false;
                    StopTrigger.Fire(this, null);
                    DebugTools.AddEvent("LoopingZoneProgram.LoopingTask.Method",
                                        "Unexpected exception in LoopingTask: " + ex.Message + " | StackTrace: " + ex.StackTrace);
                }
            }, LoopCTS.Token);
        }