public void Tick() { if (Thread.CurrentThread == thread) //Checks to ensure that we can only be run on the Thread which owns this MethodQueue { lock (_methodsToRun) //Lock the methodQueue to ensure thread safety. { while (_methodsToRun.Count > 0) //Run through all queued methods. { methodToRun thisMethod = _methodsToRun.Dequeue(); //grab this method information. thisMethod.method.Invoke(); //Run the method. if (thisMethod.source != null && thisMethod.callback != null) //If information for a callback is present, trigger the setup so that it's added to the source thread's MethodQueue. { setupCallBack(thisMethod); } } } } }
//Quick call to setup a the callback method on the _source Thread's methodQueue. private void setupCallBack(methodToRun thisMethod) { Program.methodQueues[thisMethod.source].AddMethod(thisMethod.callback); }