Esempio n. 1
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));
        }
Esempio n. 2
0
        /// <summary>
        /// Returns from an async method with whatever was last returned - Should be within a "yield return"
        /// </summary>
        /// <param name="currentThread">Current thread</param>
        /// <returns>void</returns>
        public ThreadInfo Return(ThreadInfo currentThread)
        {
            var thread = _threads[currentThread.Thread];

            _threads.Remove(currentThread.Thread);

            return(new ThreadInfo(thread.Caller, currentThread.Priority, GetResultNoException(currentThread)));
        }
Esempio 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));
        }
Esempio n. 4
0
 /// <summary>
 /// Gets the result from the last async call, but does not throw exceptions
 /// </summary>
 /// <param name="currentThread">Current thread</param>
 /// <returns>Result as an "object"</returns>
 public object GetResultNoException(ThreadInfo currentThread)
 {
     foreach (var continuation in _results)
     {
         if (continuation.Thread == currentThread.Thread)
         {
             return(continuation.Result);
         }
     }
     return(null);
 }
Esempio n. 5
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));
        }
Esempio n. 6
0
 /// <summary>
 /// Gets the result from the last async call
 /// </summary>
 /// <param name="currentThread">Current thread</param>
 /// <returns>Result as an "object"</returns>
 public object GetResult(ThreadInfo currentThread)
 {
     foreach (var continuation in _results)
     {
         if (continuation.Thread == currentThread.Thread)
         {
             if (continuation.Result is Exception)
             {
                 throw continuation.Result as Exception;
             }
             return(continuation.Result);
         }
     }
     return(null);
 }
Esempio n. 7
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);
        }
Esempio n. 8
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));
        }
Esempio n. 9
0
 /// <summary>
 /// Returns from an async method with a result - Should be within a "yield return"
 /// </summary>
 /// <param name="currentThread">Current thread</param>
 /// <param name="result">Return value of method</param>
 /// <returns>void</returns>
 public ThreadInfo Return(ThreadInfo currentThread, object result)
 {
     return(Return(currentThread).WithResult(result));
 }
Esempio n. 10
0
 /// <summary>
 /// Gets the result from the last async call
 /// </summary>
 /// <typeparam name="T">Type of the result</typeparam>
 /// <param name="currentThread">Current thread</param>
 /// <returns>Result as type T</returns>
 public T GetResult <T>(ThreadInfo currentThread)
 {
     return((T)GetResult(currentThread));
 }