从这里使用关于 lambda 运算符的答案,我有一个接受参数并且工作正常的线程。但是,我希望能够从线程中取回东西。这是代码的样子:
namespace Renamer
{
class RenameThread
{
public Thread StartRename(bool commit, ICollection checkeditems, ArrayList rules)
{
var t = new Thread(() => doRename(commit, checkeditems, rules));
t.Start();
return t;
}
private void doRename(bool commit, ICollection checkeditems, ArrayList rules)
{
ArrayList filenames = new ArrayList();
ArrayList newfilenames = new ArrayList();
filenames.AddRange(checkeditems);
//do stuff with filenames
//I want to be able to return newfilenames (or perhaps some object that contains it)
}
}
}
通过单击按钮调用它:
private void btnTest_Click(object sender, EventArgs e)
{
RenameThread rt = new RenameThread();
Thread renameThread = rt.StartRename(false, clbFiles.CheckedItems, rules);
renameThread.Join();
}
在 Java 中,我只是实现Runnable
,所以如果我需要它们,我可以直接访问线程的成员和字段,但是由于我不能从那里继承,Thread
所以我对我应该做什么有点茫然。