gpt4 book ai didi

c# - 将参数传递给 Action 调度程序

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:25 26 4
gpt4 key购买 nike

我有一个从队列中调度 Actions 的类。但是,我的 InvokeLater 只接受一个 Action,没有任何参数。

这是我的 Dispatcher:

public class Dispatcher : MonoBehaviour
{
private static readonly Queue<Action> tasks = new Queue<Action>();
public static Dispatcher Instance = null;

static Dispatcher()
{
Instance = new Dispatcher();
}

private Dispatcher(){ }

public void InvokeLater(Action task)
{
lock (tasks)
{
tasks.Enqueue(task);
}
}

void FixedUpdate()
{
while (tasks.Count > 0)
{
Action task = null;

lock (tasks)
{
if (tasks.Count > 0)
{
task = tasks.Dequeue();
}
}

task();
}
}
}

我是这样使用的:

var action = new Action(RequestPrediction);
Dispatcher.Instance.InvokeLater(action);

其中 RequestPrediction 是一个无参数函数——我想对其进行更改,以便它 (RequestPrediction) 采用一个 int 参数。

如何更改我的 Dispatcher 以便我可以执行以下操作:

var action = new Action(RequestPrediction);
Dispatcher.Instance.InvokeLater(action,5);

?

最佳答案

最简单的解决方案:

var action = new Action(() => RequestPrediction(5));
Dispatcher.Instance.InvokeLater(action);

否则,您将不得不开发自己的框架,并且最终会得到一些看起来像现有的 TPL library in .NET 的东西。或现有 Dispatcher implementation .还有其他更好的建议。

关于c# - 将参数传递给 Action 调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com