Ejemplo n.º 1
0
        public void CreateWithCancellationToken()
        {
            using (var source = new CancellationTokenSource())
            {
                var token = new CancelScriptToken(source.Token);

                Assert.IsFalse(token.IsCancellationRequested);

                source.Cancel();
                Assert.IsTrue(token.IsCancellationRequested);
            }
        }
Ejemplo n.º 2
0
        private void CreateScriptScope(string tokenName, CancelScriptToken token)
        {
            CreateScriptScope();

            // Add the token to the available elements list
            if (m_Scope.ContainsVariable(tokenName))
            {
                m_Scope.RemoveVariable(tokenName);
            }

            m_Scope.SetVariable(tokenName, token);
        }
Ejemplo n.º 3
0
        public System.Tuple<Task, CancellationTokenSource> Execute(ScriptLanguage language, string scriptCode, TextWriter outputChannel)
        {
            // If there is an existing runner then nuke that one
            if (m_CurrentlyRunningScript != null)
            {
                throw new CannotInterruptRunningScriptException();
            }

            if ((m_CurrentLanguageDomainPair != null) && (m_CurrentLanguageDomainPair.Item1 != language))
            {
                // Different language requested so nuke the domain
                UnloadCurrentScriptDomain();
            }

            if (m_CurrentLanguageDomainPair == null)
            {
                var scriptDomain = m_AppDomainBuilder("ScriptDomain", AppDomainPaths.Core);
                m_CurrentLanguageDomainPair = new System.Tuple<ScriptLanguage, AppDomain>(language, scriptDomain);
            }

            IExecuteScripts executor = LoadExecutor(language, m_CurrentLanguageDomainPair.Item2, outputChannel);

            var source = new CancellationTokenSource();
            var token = new CancelScriptToken(source.Token);
            var result = Task.Factory.StartNew(
                () =>
                {
                    try
                    {
                        executor.Execute(scriptCode, token);
                    }
                    finally
                    {
                        m_CurrentlyRunningScript = null;
                        if (m_CurrentToken != null)
                        {
                            m_CurrentToken.Dispose();
                            m_CurrentToken = null;
                        }
                    }
                },
                source.Token,
                TaskCreationOptions.LongRunning,
                m_Scheduler);

            m_CurrentlyRunningScript = result;
            m_CurrentToken = source;

            return new System.Tuple<Task, CancellationTokenSource>(result, source);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Executes the given script.
 /// </summary>
 /// <param name="scriptCode">The script code.</param>
 /// <param name="token">A cancellation token that can be used to interrupt the script.</param>
 public void Execute(string scriptCode, CancelScriptToken token)
 {
     CreateScriptScope(ScriptCancellationToken, token);
     ExecuteScript(scriptCode);
 }