创建 MethodInvoker 的队列
Queue<MethodInvoker> EventCall = new Queue<MethodInvoker>();
稍后将项目添加到您的队列
EventCall.Enqueue(ClearAllVals);
EventCall.Enqueue(saystuff);
EventCall.Enqueue(testFunc);
然后一次调用一个函数:
MethodInvoker bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();
以安全的方式调用所有函数(这也会将它们全部从队列中删除,使队列为空并调用所有函数)
public bool InvokeAll(){
MethodInvoker bb = null; // this will hold the function prior to use
for(int i = 0; i<EventCall.count; i++){
bb = EventCall.Dequeue(); //pull a method off of the queue
bb(); //call the method you pulled off of the queue
}
}
要调用它们,只需使用InvokeAll();
或随时调用它们:
public bool NextEvent(){
MethodInvoker bb = null; // this will hold the function prior to use
if(EventCall.count > 0){
bb = EventCall.Dequeue(); //pull a method off of the queue
bb(); //call the method you pulled off of the queue
} else {
MessageBox.Show("there was no event to call"); // this is optional, point being you should be handeling the fact that there is no events left in some way.
}
}