public void ExecuteThreadWork(object obj) { running = true; if (workerObject == null || waitHandle == null) { return; } //Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal; if (safeMode) { try { workerObject.ExecuteThreadedWork(); } catch (Exception e) { //safeModeErrorFound = true; //safeModeErrorLog.Add(e.Message + e.StackTrace); UThread.DispatchToMainThread(() => Debug.LogError(e.Message + e.StackTrace + "\n\n"), true); } } else { workerObject.ExecuteThreadedWork(); } running = false; finishedWorking = true; waitHandle.Set(); //Fire back to the MainThread! }
private void Awake() { self = this; DontDestroyOnLoad(this.gameObject); UThread.initUnityThread(); accountparser = new INIParser(); string path = Application.dataPath.Replace("/", "\\").Replace("\\Assets", string.Empty); accountparser.Open(path + "\\account.dat", true); if (!accountparser.IsKeyExists("basic", "username")) { accountparser.WriteValue("basic", "username", "admin"); } if (!accountparser.IsKeyExists("basic", "password")) { accountparser.WriteValue("basic", "password", "admin"); } var date = DateTime.Now; var formattedDate = string.Format("{0}/{1}/{2}", date.Month, date.Day, date.Year); var time = new DateTime(DateTime.Now.TimeOfDay.Ticks); // Date part is 01-01-0001 var formattedTime = formattedDate + "-" + time.ToString("h:mm:sstt", CultureInfo.InvariantCulture); accountparser.WriteValue("basic", "lastlogin", formattedTime); }
public void SafeExecte_ParamThreadStart(object argument) { try { paramTargetMethod(argument); } catch (Exception e) { UThread.DispatchToMainThread(() => Debug.LogError(e.Message + e.StackTrace + "\n\n"), true); } }
public void SafeExecte_ThreadStart() { try { targetMethod(); } catch (Exception e) { UThread.DispatchToMainThread(() => Debug.LogError(e.Message + e.StackTrace + "\n\n"), true); } }
void Start() { DontDestroyOnLoad(this); myThreadScheduler = UThread.CreateThreadPoolScheduler(); //myThreadScheduler.ForceToMainThread = true; //UThread.StartSingleThread(LoadConfig, System.Threading.ThreadPriority.Normal, true); workerObjects = new FlockingDataWorker[1]; int maxThreads = -1; if (maxThreads <= 0) { maxThreads = Mathf.Max(SystemInfo.processorCount - 1, 1); } FlockingDataWorker workerBlock = new FlockingDataWorker(); workerObjects[0] = workerBlock; myThreadScheduler.ForceToMainThread = true; myThreadScheduler.StartASyncThreads(workerObjects, onThreadWorkComplete, null, maxThreads); StartCoroutine(LoadPosFile()); StartCoroutine(LoadSummonPosFile()); }
/// <summary> /// Helps spreading the same repetetive workload over multiple threads/cores in an easy way. /// </summary> /// <typeparam name="D">Generic-Type of the delegate that will be executed and compute the workload</typeparam> /// <typeparam name="T">Generic-Type of the object you want to be computed by the executor</typeparam> /// <param name="executor">A (static) method that computes one workLoad-object at a time</param> /// <param name="workLoad">An array with objects you want to get computed by the executor</param> /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param> /// <param name="onPackageComplete">Fires foreach finished re-packaged set of workLoad-object</param> /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one core to run on. (quadcore == 1 core Mainthread, 3 cores used by the ThreadPoolScheduler)</param> /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> /// <returns>A ThreadPoolScheduler that handles all the repackaged workLoad-Objects =</returns> public static ThreadPoolScheduler StartMultithreadedWorkloadExecution <D, T>(D executor, T[] workLoad, object extraArgument, MultithreadedWorkloadComplete <T> onComplete, MultithreadedWorkloadPackageComplete <T> onPackageComplete, int maxThreads = -1, ThreadPoolScheduler scheduler = null, bool safeMode = true) { if (scheduler == null) { scheduler = UThread.CreateThreadPoolScheduler(); } else if (scheduler.isBusy) { Debug.LogError("Provided Scheduler stil busy!!!"); } if (maxThreads <= 0) { maxThreads = Mathf.Max(SystemInfo.processorCount - 1, 1); } int packagesPerThread = 1; if (maxThreads > 1) //If we are running in just one thread at a time, just use one, if more, for sake of better cpu-saturation, subdive into smaller packages per core. { packagesPerThread = 2; } int packages = Mathf.Min(maxThreads * packagesPerThread, workLoad.Length); int objectsPerPackage = (int)Mathf.Ceil((float)workLoad.Length / (float)packages); ThreadWorkDistribution <T>[] workerPackages = new ThreadWorkDistribution <T> [packages]; Type delegateType = typeof(D); //Debug.Log(delegateType.FullName); int count = 0; for (int i = 0; i < packages; i++) { int packagedSize = Mathf.Min(workLoad.Length - count, objectsPerPackage); if (delegateType == typeof(ThreadWorkloadExecutor <T>)) { workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutor <T>), workLoad, count, count + packagedSize); } else if (delegateType == typeof(ThreadWorkloadExecutorIndexed <T>)) { workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutorIndexed <T>), workLoad, count, count + packagedSize); } else if (delegateType == typeof(ThreadWorkloadExecutorArg <T>)) { workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutorArg <T>), workLoad, extraArgument, count, count + packagedSize); } else if (delegateType == typeof(ThreadWorkloadExecutorArgIndexed <T>)) { workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutorArgIndexed <T>), workLoad, extraArgument, count, count + packagedSize); } workerPackages[i].ID = i; count += objectsPerPackage; } //--------------- Store session data -------------------- ThreadWorkDistributionSession <T> sessionData = new ThreadWorkDistributionSession <T>(); sessionData.workLoad = workLoad; sessionData.onComplete = onComplete; sessionData.onPackageComplete = onPackageComplete; sessionData.packages = workerPackages; //--------------- Store session data -------------------- scheduler.StartASyncThreads(workerPackages, sessionData.onCompleteBubble, sessionData.onPackageCompleteBubble, maxThreads, safeMode); return(scheduler); }