public int AddThread(IGenThread genThread, String threadNameSuffix = null, IGenThread parentGenThread = null, bool autoRestart = false)
        {
            // add thread to thread list
            GenThreadInfo info = new GenThreadInfo();
            info.Name = threadNameSuffix == null ? genThread.ThreadName : (genThread.ThreadName + "_" + threadNameSuffix);
            info.GenThread = genThread;
            if (parentGenThread != null)
                info.ParentGenThreadInfo = FindThread(parentGenThread);
            info.Thread = new Thread(info.GenThread.RunThread);
            info.Thread.Name = info.Name;
            info.StartTime = null;
            info.AutoRestart = autoRestart;
            info.StartCount = 0;

            Threads.Add(info);

            return info.GenThread.ThreadNo;
        }
Example #2
0
 private void Update()
 {
     //return data back to chunks
     if (chunkDataThreadInfoQueue.Count > 0)
     {
         for (int i = 0; i < chunkDataThreadInfoQueue.Count; i++)
         {
             GenThreadInfo <ChunkData> threadInfo = chunkDataThreadInfoQueue.Dequeue();
             threadInfo.callback(threadInfo.parameter);
         }
     }
     if (meshDataThreadInfoQueue.Count > 0)
     {
         for (int i = 0; i < meshDataThreadInfoQueue.Count; i++)
         {
             GenThreadInfo <MeshData> threadInfo = meshDataThreadInfoQueue.Dequeue();
             threadInfo.callback(threadInfo.parameter);
         }
     }
 }
        private void StopThread(GenThreadInfo info, bool forceStop = false, bool deleteThread = false)
        {
            // stop one thread if still marked as running
            if (info.Thread != null )
            {
                // tell GenThread to exit - polite request to stop (including child threads)
                info.AutoRestart = false;
                info.GenThread.IsRunning = false;
                info.GenThread.ExitEvent.Set();

                // force the thread to stop - used to clean up stopped threads and threads with bad behaviour
                if (forceStop)
                {
                    if (!info.Thread.Join(5000))
                    {
                        SystemServices.LogMessage("GenThreadManager.StopThread", "Killing thread: " + info.Name, LogEntryType.ErrorMessage);
                        info.Thread.Interrupt();
                    }
                }
            }
            if (deleteThread && forceStop)
                Threads.Remove(info);
        }
 private void StartThread(GenThreadInfo info)
 {
     // GenThread will exit when ExitEvent is Set
     info.GenThread.ExitEvent.Reset();
     info.GenThread.IsRunning = true;
     info.Thread.Start();
     info.StartTime = DateTime.Now;
     info.StartCount++;
 }
 private void RestartThread(GenThreadInfo info)
 {
     info.Thread = new Thread(info.GenThread.RunThread);
     info.Thread.Name = info.Name;
     StartThread(info);
     SystemServices.LogMessage("GenThreadManager.RestartThread", info.Name + " restarted as thread: " + info.GenThread.ThreadNo, LogEntryType.Trace);
 }