gpt4 book ai didi

c# - 如何传入特定类的方法名作为另一个方法的参数

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:06 24 4
gpt4 key购买 nike

我正在尝试编写一个 C# 方法,该方法通过传入一个对象并运行其方法之一来生成线程。

这是一个过于简单的例子:

class ThreadData {
private JObject jsonObj;
private int start;
private int end;
public ThreadData(JObject jsonObj, int start, int end) {
this.jsonObj= jsonObj;
this.start = start;
this.end = end;
}
public void Calculate() {
for (int i = start; i < end; i++) {
// do stuff
}
}
public void Produce() {
for (int i = start; i < end; i++) {
// do other stuff
}
}
}

class Program {
static void Main(string[] args) {
JObject jsonObj= SomeRestAPICall();
RunInThreads(5, jsonObj, Calculate);
RunInThreads(5, jsonObj, Produce);
}

// ...
}

json 对象有 n 个项目。 RunInThreads 方法的目标是生成 X 个线程,将 jsonObj 分成相等的 X 个 block ,并让每个线程在每个 block 上运行。我将线程数、json 对象以及运行 ThreadData 类的方法传递给该方法。

private static void RunInThreads(int numOfThreads, JObject object, type functionToRun) {
int chunks = object.Count() / numOfThreads;
List<Thread> threads = new List<Thread>();
for (int i = 1; i <= numOfThreads; i++) {
int start = (i - 1) * chunks;
int end = i == numOfThreads? object.Count() : start + chunks;

ThreadData threadData = new ThreadData(object, start, end);
Thread thread = new Thread(new ThreadStart(threadData.<FUNCTION>));
thread.Start();
threads.Add(thread);
}
foreach (Thread t in threads) { t.Join(); }
}

我不太明白如何将函数名作为参数传入。我会使用委托(delegate)吗?如果我使用两种不同的方法,我会在每个方法中输入自定义方法名称。例如,对于 Calculate() 方法,它将是这样的:

private static void CalculateInThreads(int numOfThreads, JObject object) {
int chunks = object.Count() / numOfThreads;
List<Thread> threads = new List<Thread>();
for (int i = 1; i <= numOfThreads; i++) {
int start = (i - 1) * chunks;
int end = i == numOfThreads? object.Count() : start + chunks;

ThreadData threadData = new ThreadData(object);
Thread thread = new Thread(new ThreadStart(threadData.functionToRun));
thread.Start();
threads.Add(thread);
}
foreach (Thread t in threads) { t.Join(); }
}

最佳答案

我相信这样的事情会奏效:

private static void RunInThreads(int numOfThreads, JObject object, 
Action<ThreadData> functionToRun) {
int chunks = object.Count() / numOfThreads;
List<Thread> threads = new List<Thread>();
for (int i = 1; i <= numOfThreads; i++) {
int start = (i - 1) * chunks;
int end = i == numOfThreads? object.Count() : start + chunks;

ThreadData threadData = new ThreadData(object, start, end);
Thread thread = new Thread(new ThreadStart(() => functionToRun(threadData));
thread.Start();
threads.Add(thread);
}
foreach (Thread t in threads) { t.Join(); }
}

你会这样调用它:

RunInThreads(5, jsonObj, t=>t.Calculate());

语法解释:

Action<ThreadData>是一个 Action<T> - 一个预定义委托(delegate),接受一个输入参数并返回 void。

t => t.Calculate()语法是 lambda expression表示委托(delegate)的一个实例 - 它基本上是定义一个采用单个参数的内联函数的简短方法 t . t 的类型推断为 ThreadData因为编译器知道委托(delegate)是一个 Action<ThreadData> .

你可以像这样用更多的按键来写同样的东西:

void DoCalculate(ThreadData data)
{
data.Calculate();
}
...
RunInThread(5, jsonObj, new Action<ThreadData>(DoCalculate));

关于c# - 如何传入特定类的方法名作为另一个方法的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870947/

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