Ejemplo n.º 1
0
        /// <summary>
        /// Waits for a SQL stored procedure to execute - Should be within a "yield return"
        /// </summary>
        /// <param name="currentThread">Current thread</param>
        /// <param name="connectionString">String to connect to the database</param>
        /// <param name="procedure">Name of stored procedure</param>
        /// <param name="parameters">List of parameters to the procedure</param>
        /// <returns>DataTable - Result from the stored procedure</returns>
        public ThreadInfo MakeDbCall(ThreadInfo currentThread, string connectionString, string procedure, params SqlParameter[] parameters)
        {
            IncreaseExecutions(currentThread.Priority);
            Task.Factory.StartNew <object>(() =>
            {
                try
                {
                    using (var connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        var table = new DataTable();
                        using (var command = connection.CreateCommand())
                        {
                            command.CommandType = CommandType.StoredProcedure;
                            command.CommandText = procedure;
                            command.Parameters.AddRange(parameters);
                            using (var reader = command.ExecuteReader())
                            {
                                table.Load(reader);
                            }
                            return(table);
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(ex);
                }
            }).ContinueWith((task) =>
                            Enqueue(currentThread.WithResult(task.Result)));

            return(new ThreadInfo(currentThread.Priority));
        }
Ejemplo n.º 2
0
        public ThreadInfo StartNewThread(ThreadInfo currentThread, Func <object> thread)
        {
            IncreaseExecutions(currentThread.Priority);
            Task.Factory.StartNew <object>(thread).ContinueWith((task) =>
                                                                Enqueue(currentThread.WithResult(task.Result)));

            return(new ThreadInfo(currentThread.Priority));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Waits for input from the console - Should be within a "yield return"
        /// </summary>
        /// <param name="currentThread">Current thread</param>
        /// <returns>string - Input from the console</returns>
        public ThreadInfo WaitForConsole(ThreadInfo currentThread)
        {
            IncreaseExecutions(currentThread.Priority);
            Task.Factory.StartNew <object>(() =>
            {
                return(Console.ReadLine());
            }).ContinueWith((task) =>
                            Enqueue(currentThread.WithResult(task.Result)));

            return(new ThreadInfo(currentThread.Priority));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sleeps for a specified amount of time - Should be within a "yield return"
        /// </summary>
        /// <param name="currentThread">Current thread</param>
        /// <param name="milliseconds">Time to sleep in milliseconds</param>
        /// <returns>string - String specifying how long the thread slept</returns>
        public ThreadInfo Sleep(ThreadInfo currentThread, int milliseconds)
        {
            IncreaseExecutions(currentThread.Priority);
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(milliseconds);
                return("Slept for " + milliseconds + " milliseconds.");
            }).ContinueWith((task) =>
                            Enqueue(currentThread.WithResult(task.Result)));

            return(new ThreadInfo(currentThread.Priority));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Makes a synchronous call to an async method - Should be within a "yield return"
        /// </summary>
        /// <param name="currentThread">Current thread</param>
        /// <param name="method">Method to call</param>
        /// <returns>void</returns>
        public ThreadInfo Await(ThreadInfo currentThread, IEnumerable <ThreadInfo> method)
        {
            var thread   = method.GetEnumerator();
            var threadId = _threads.Insert(new IteratorInfo(currentThread.Thread, thread));

            CurrentThread = new ThreadInfo(threadId, currentThread.Priority);
            try
            {
                thread.MoveNext();
            }
            catch (Exception ex)
            {
                return(currentThread.WithResult(ex));
            }
            return(thread.Current);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Waits for a client using an HttpListener - Should be within a "yield return"
        /// </summary>
        /// <param name="currentThread">Current thread</param>
        /// <param name="listener">HTTPListener</param>
        /// <returns>HttpListenerContext - Client found</returns>
        public ThreadInfo WaitForClient(ThreadInfo currentThread, HttpListener listener)
        {
            IncreaseExecutions(currentThread.Priority);
            Task.Factory.StartNew <object>(() =>
            {
                try
                {
                    return(listener.GetContext());
                }
                catch (Exception ex)
                {
                    return(ex);
                }
            }).ContinueWith((task) =>
                            Enqueue(currentThread.WithResult(task.Result)));

            return(new ThreadInfo(currentThread.Priority));
        }