Exemplo n.º 1
0
        public void StartProgram(string options)
        {
            if (programBlock.IsRunning)
            {
                return;
            }

            // TODO: since if !program.IsRunning also thread should be null
            // TODO: so this is probably useless here and could be removed?
            if (programThread != null)
            {
                StopProgram();
            }

            programBlock.IsRunning = true;
            homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, "Running");

            programBlock.TriggerTime = DateTime.UtcNow;

            programThread = new Thread(() =>
            {
                try
                {
                    MethodRunResult result = null;
                    try
                    {
                        result = programBlock.Run(options);
                    }
                    catch (Exception ex)
                    {
                        result           = new MethodRunResult();
                        result.Exception = ex;
                    }
                    programThread          = null;
                    programBlock.IsRunning = false;
                    if (result != null && result.Exception != null && !result.Exception.GetType().Equals(typeof(System.Reflection.TargetException)))
                    {
                        // runtime error occurred, script is being disabled
                        // so user can notice and fix it
                        List <ProgramError> error = new List <ProgramError>()
                        {
                            programBlock.GetFormattedError(result.Exception, false)
                        };
                        programBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        programBlock.IsEnabled    = false;
                        homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.RuntimeError, "CR: " + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' '));
                    }
                    homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, programBlock.IsEnabled ? "Idle" : "Stopped");
                }
                catch (ThreadAbortException)
                {
                    programThread          = null;
                    programBlock.IsRunning = false;
                    homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, "Interrupted");
                }
            });

            if (programBlock.ConditionType == ConditionType.Once)
            {
                programBlock.IsEnabled = false;
            }

            try
            {
                programThread.Start();
            }
            catch
            {
                StopProgram();
                homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.ProgramStatus, "Idle");
            }
        }
Exemplo n.º 2
0
        public void StartProgram(string options)
        {
            if (ProgramBlock.IsRunning)
            {
                return;
            }

            // TODO: since if !program.IsRunning also thread should be null
            // TODO: so this is probably useless here and could be removed?
            if (_programThread != null)
            {
                StopProgram();
            }

            ProgramBlock.IsRunning = true;
            Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus, "Running");

            ProgramBlock.TriggerTime = DateTime.UtcNow;

            _programThread = new Thread(() =>
            {
                try
                {
                    MethodRunResult result;
                    try
                    {
                        result = ProgramBlock.Run(options);
                    }
                    catch (Exception ex)
                    {
                        result = new MethodRunResult {
                            Exception = ex
                        };
                    }
                    _programThread         = null;
                    ProgramBlock.IsRunning = false;
                    if (result != null && result.Exception != null && !result.Exception.GetType()
                        .Equals(typeof(System.Reflection.TargetException)))
                    {
                        // runtime error occurred, script is being disabled
                        // so user can notice and fix it
                        var error = new List <ProgramError> {
                            ProgramBlock.GetFormattedError(result.Exception, false)
                        };
                        ProgramBlock.ScriptErrors = JsonConvert.SerializeObject(error);
                        _log.Error(result.Exception, "Error while running program {0}", ProgramBlock.Address);
                        Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.RuntimeError,
                                                                         PrepareExceptionMessage(CodeBlockEnum.CR, result.Exception));

                        TryToAutoRestart();
                    }
                    Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus, ProgramBlock.IsEnabled ? "Idle" : "Stopped");
                }
                catch (ThreadAbortException)
                {
                    _programThread         = null;
                    ProgramBlock.IsRunning = false;
                    if (Homegenie.ProgramManager != null)
                    {
                        Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus, "Interrupted");
                    }
                }
            });

            if (ProgramBlock.ConditionType == ConditionType.Once)
            {
                ProgramBlock.IsEnabled = false;
            }

            try
            {
                _programThread.Start();
            }
            catch
            {
                StopProgram();
                Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.ProgramStatus, "Idle");
            }
            lastProgramRunTs = DateTime.Now;
        }