C# 在新线程上运行简单函数的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1603263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to run a simple function on a new Thread?
提问by Malfist
I have two functions that I want to run on different threads (because they're database stuff, and they're not needed immediately).
我有两个要在不同线程上运行的函数(因为它们是数据库的东西,并且不需要立即使用)。
The functions are:
功能是:
getTenantReciept_UnitTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_Unit);
getTenantReciept_TenantNameTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_TenantName);
In javascript, I know I can create create an anonymous function and call it on a new thread quite easily with something like this:
在 javascript 中,我知道我可以创建一个匿名函数并很容易地在一个新线程上调用它,如下所示:
setTimeout(new function(){doSomethingImportantInBackground();}, 500);
Is there something like this in C#?
C#中有这样的东西吗?
采纳答案by Jon Skeet
Your question isn't very clear, I'm afraid. You can easily start a new thread with some code, using anonymous methods in C# 2, and lambda expressions in C# 3:
恐怕你的问题不是很清楚。您可以使用一些代码轻松启动一个新线程,使用 C# 2 中的匿名方法和 C# 3 中的 lambda 表达式:
Anonymous method:
匿名方法:
new Thread(delegate() {
getTenantReciept_UnitTableAdapter1.Fill(
rentalEaseDataSet1.GetTenantReciept_Unit);
}).Start();
new Thread(delegate() {
getTenantReciept_TenantNameTableAdapter1.Fill(
rentalEaseDataSet1.GetTenantReciept_TenantName);
}).Start();
Lambda expression:
拉姆达表达式:
new Thread(() =>
getTenantReciept_UnitTableAdapter1.Fill(
rentalEaseDataSet1.GetTenantReciept_Unit)
).Start();
new Thread(() =>
getTenantReciept_TenantNameTableAdapter1.Fill(
rentalEaseDataSet1.GetTenantReciept_TenantName)
).Start();
You can use the same sort of syntax for Control.Invoke
, but it's slightly trickier as that can take anydelegate - so you need to tell the compiler which type you're using rather than rely on an implicit conversion. It's probably easiest to write:
您可以对 使用相同类型的语法Control.Invoke
,但它有点棘手,因为它可以接受任何委托 - 因此您需要告诉编译器您使用的是哪种类型,而不是依赖于隐式转换。可能最简单的写法是:
EventHandler eh = delegate
{
// Code
};
control.Invoke(eh);
or
或者
EventHandler eh = (sender, args) =>
{
// Code
};
control.Invoke(eh);
As a side note, are your names really that long? Can you shorten them to get more readable code?
作为旁注,你的名字真的那么长吗?你能缩短它们以获得更易读的代码吗?
回答by Daniel Rodriguez
You could use an anonymous method:
您可以使用匿名方法:
void Foo()
{
Thread myThread = new System.Threading.Thread(delegate(){
//Your code here
});
myThread.Start();
}
回答by oefe
Starting threads is relatively expensive.
启动线程相对昂贵。
You might be better of using a thread from the thread pool:
使用线程池中的线程可能会更好:
ThreadPool.QueueUserWorkItem(unused =>
getTenantReciept_UnitTableAdapter1.Fill(
rentalEaseDataSet1.GetTenantReciept_Unit)
);
ThreadPool.QueueUserWorkItem(unused =>
getTenantReciept_TenantNameTableAdapter1.Fill(
rentalEaseDataSet1.GetTenantReciept_TenantName)
);
回答by JSideris
Similar to what's been said - I find tasks to be a bit simpler (supported as of .net 4and can be used as follows as of .net 4.5):
类似于所说的 - 我发现任务更简单一些(从.net 4 开始支持,从.net 4.5 开始可以按如下方式使用):
Task mytask = Task.Run(() =>
{
//Lines of code
});