Пример #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);
            }
        }
Пример #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);
        }
Пример #3
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);
            }
        }
Пример #4
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));
        }
Пример #5
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);
 }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
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);
 }