Exemplo n.º 1
0
		/* Function: Multithread
		 * 
		 * Executes the task across multiple threads.  The function passed must be suitably thread safe.  This
		 * function will not return until the task is complete.
		 * 
		 * Parameters:
		 * 
		 *		threadName - What the execution threads should be named.  "Thread #" will be appended so "Builder" will lead to 
		 *								 names like "Builder Thread 3".  This is important to specify because thread names are reported in 
		 *								 exceptions and crash reports.
		 *		task - The task to execute.  This must be thread safe.
		 */
		static private void Multithread (string threadName, CancellableTask task)
			{
			if (workerThreadCount == 1)
				{
				// If there's only one thread, execute it on the main thread instead of spawning a new one.
				// Uses fewer resources and makes debugging easier.
				task(Engine.Delegates.NeverCancel);
				}
			else
				{
				Engine.Thread[] threads = new Engine.Thread[workerThreadCount];

				for (int i = 0; i < threads.Length; i++)
					{  
					Engine.Thread thread = new Engine.Thread();

					thread.Name = threadName + " Thread " + (i + 1);
					thread.Task = task;
					thread.CancelDelegate = Engine.Delegates.NeverCancel;
					thread.Priority = System.Threading.ThreadPriority.BelowNormal;

					threads[i] = thread;
					}

				foreach (var thread in threads)
					{  thread.Start();  }

				foreach (var thread in threads)
					{  thread.Join();  }

				foreach (var thread in threads)
					{  thread.ThrowExceptions();  }
				}
			}
Exemplo n.º 2
0
        public Thread(System.Exception exception, Engine.Thread thread)
            : base("The thread has ended due to an exception", exception)
        {
            threadStackTrace = exception.StackTrace;
            utcTime          = System.DateTime.UtcNow;

            threadName = thread.Name;
        }