/// <summary> /// Add request to task Queue, that way it can be run (with delegate to run after!). /// </summary> /// <param name="method"> Method you want to call, format in lambda AKA like: () => {method in question} </param> /// <param name="callback">What to call after the call has been completed</param> /// <returns>Returns A task that will contain the value returned, a task can also in theory be re-added in the future</returns> public ThreadTaskRequest AddRequest(Func <object> method, Action <CallbackArgs <object> > callback = null) { if (method == null) { throw new ArgumentNullException(nameof(method)); } ThreadTaskRequest taskClass = new ThreadTaskRequest(method, callback); PooledThreadClass lowestTaskThread = null; int lowestTaskNumber = int.MaxValue; foreach (PooledThreadClass threadClass in _pooledThreadClasses) { if (lowestTaskNumber > threadClass.TasksAssigned.Count) { lowestTaskNumber = threadClass.TasksAssigned.Count; lowestTaskThread = threadClass; } } if (lowestTaskThread?.TaskAccessLock != null) { lock (lowestTaskThread?.TaskAccessLock) { lowestTaskThread?.TasksAssigned.Add(taskClass); } } return(taskClass); }
/// <summary> /// Sets the max thread count after intialization /// </summary> /// <param name="threadCount">Threads to allocate</param> public void SetMaxThreadCount(byte threadCount) { _pooledThreadClasses = new PooledThreadClass[threadCount]; for (int i = 0; i < Threads; i++) { _pooledThreadClasses[i] = new PooledThreadClass(); } }