public void StartScript() { if (homegenie == null || eventItem == null || isRunning || String.IsNullOrWhiteSpace(eventItem.Script)) { return; } if (programThread != null) { StopScript(); } isRunning = true; homegenie.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, eventItem.Name, Properties.SchedulerScriptStatus, eventItem.Name + ":Start"); programThread = new Thread(() => { try { MethodRunResult result = null; try { scriptEngine.Execute(InitScript + eventItem.Script); } catch (Exception ex) { result = new MethodRunResult(); result.Exception = ex; } programThread = null; isRunning = false; if (result != null && result.Exception != null && result.Exception.GetType() != typeof(TargetException)) { homegenie.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, eventItem.Name, Properties.SchedulerScriptStatus, eventItem.Name + ":Error (" + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' ') + ")"); } } catch (ThreadAbortException) { programThread = null; isRunning = false; homegenie.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, eventItem.Name, Properties.SchedulerScriptStatus, eventItem.Name + ":Interrupted"); } homegenie.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, eventItem.Name, Properties.SchedulerScriptStatus, eventItem.Name + ":End"); }); try { programThread.Start(); } catch { StopScript(); } }
public void StartScript() { if (_hgService == null || _eventItem == null || _isRunning || string.IsNullOrWhiteSpace(_eventItem.Script)) { return; } if (_programThread != null) { StopScript(); } _isRunning = true; _hgService.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, _eventItem.Name, Properties.SchedulerScriptStatus, _eventItem.Name + ":Start"); _programThread = new Thread(() => { try { MethodRunResult result = null; try { _scriptEngine.Execute(InitScript + _eventItem.Script); } catch (Exception ex) { result = new MethodRunResult { Exception = ex }; } _programThread = null; _isRunning = false; if (result != null && result.Exception != null && !result.Exception.GetType().Equals(typeof(System.Reflection.TargetException))) { _hgService.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, _eventItem.Name, Properties.SchedulerScriptStatus, "Error (" + result.Exception.Message.Replace('\n', ' ').Replace('\r', ' ') + ")"); } } catch (ThreadAbortException) { _programThread = null; _isRunning = false; _hgService.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, _eventItem.Name, Properties.SchedulerScriptStatus, "Interrupted"); } _hgService.RaiseEvent(this, Domains.HomeAutomation_HomeGenie, SourceModule.Scheduler, _eventItem.Name, Properties.SchedulerScriptStatus, "End"); }); try { _programThread.Start(); } catch { StopScript(); } }