目前,我正在工作:
public void logowanie()
{
int x=5,y=5;
...
}
private void button2_Click(object sender, EventArgs e)
{
Thread thread2 = new Thread(new ThreadStart(logowanie));
thread2.Start();
//logowanie("xd", "xd", "xd");
}
那行得通。有可能做类似的事情吗
public int logowanie(int x, int y)
{
...
}
private void button2_Click(object sender, EventArgs e)
{
Thread thread2 = new Thread(new ThreadStart(logowanie(5,5)));
thread2.Start();
//logowanie("xd", "xd", "xd");
}
我刚刚开始进行线程处理。谢谢
最佳答案
尽管您可以使用ParameterizedThreadStart
,但我可能只使用一个lambda表达式:
private void button2_Click(object sender, EventArgs e)
{
Thread thread2 = new Thread(() => logowanie(5, 5));
thread2.Start();
}
请注意,如果您在循环中调用此函数,则需要小心,因为lambda表达式捕获变量的方式是:
// Broken (before C# 5)
foreach (string url in urls)
{
new Thread(() => CrawlUrl(url));
}
// Fixed
foreach (string url in urls)
{
string copy = url;
new Thread(() => CrawlUrl(copy));
}
这仅在某些情况下是一个问题,在这种情况下,您正在捕获变量并且确实想捕获当前值,但这是值得了解的。