如何使用参数调用线程中的方法并返回一些值
我喜欢在带有参数的线程中调用该方法并在此处返回一些值示例
class Program
{
static void Main()
{
Thread FirstThread = new Thread(new ThreadStart(Fun1));
Thread SecondThread = new Thread(new ThreadStart(Fun2));
FirstThread.Start();
SecondThread.Start();
}
public static void Fun1()
{
for (int i = 1; i <= 1000; i++)
{
Console.WriteLine("Fun1 writes:{0}", i);
}
}
public static void Fun2()
{
for (int i = 1000; i >= 6; i--)
{
Console.WriteLine("Fun2 writes:{0}", i);
}
}
}
我知道上面的示例运行成功,但是如果像这样的方法 fun1
public int fun1(int i,int j)
{
int k;
k=i+j;
return k;
}
那么我如何在线程中调用它?
I like to call the method in thread with arguments and return some value here example
class Program
{
static void Main()
{
Thread FirstThread = new Thread(new ThreadStart(Fun1));
Thread SecondThread = new Thread(new ThreadStart(Fun2));
FirstThread.Start();
SecondThread.Start();
}
public static void Fun1()
{
for (int i = 1; i <= 1000; i++)
{
Console.WriteLine("Fun1 writes:{0}", i);
}
}
public static void Fun2()
{
for (int i = 1000; i >= 6; i--)
{
Console.WriteLine("Fun2 writes:{0}", i);
}
}
}
I know this above example run successfully but if method fun1 like this
public int fun1(int i,int j)
{
int k;
k=i+j;
return k;
}
then how can I call this in thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这可能是另一种方法。这里输入作为参数化线程传递,返回类型在委托事件中传递,以便当线程完成时将调用委托。当线程完成时,这将很好地得到结果。
This may be another approach. Here input is passed as parameterized Thread and return type is passed in the delegate event, so that when the thread complete that will call the Delegate. This will be fine to get result when the thread completes.
我喜欢马克·格拉维尔的回答。您只需稍加修改即可将结果传递回主线程:
I like Mark Gravell's answer. You can pass your result back out to the main thread with just a little modification:
在单独的线程中执行函数有更简单的方法:
该函数将在线程池线程上执行,并且该逻辑对您的应用程序完全透明。
一般来说,我建议在线程池而不是专用线程上执行此类小任务。
There is much simpler way to execute function in separate thread:
This function will be executed on thread pool thread, and that logic is completely transparent to your application.
An in general, I suggest to execute such small tasks on thread pool instead of dedicated thread.
您可以在 Thread 构造函数上使用 ParameterizedThreadStart 重载。它允许您将对象作为参数传递给线程方法。它将是单个对象参数,因此我通常为此类线程创建一个参数类。该对象还可以存储线程执行的结果,您可以在线程结束后读取该结果。
不要忘记,在线程运行时访问该对象是可能的,但不是“线程安全的”。你知道该怎么做:)
这是一个例子:
You can use the ParameterizedThreadStart overload on the Thread constructor. It allows you to pass an Object as a parameter to your thread method. It's going to be a single Object parameter, so I usually create a parameter class for such threads.. This object can also store the result of the thread execution, that you can read after the thread has ended.
Don't forget that accessing this object while the thread is running is possible, but is not "thread safe". You know the drill :)
Here's an example:
对于一些替代方案; currying:
或者编写您自己的捕获类型(这与您使用带有捕获变量的非方法/ lambda 时编译器为您所做的事情大致相当,但实现方式有所不同):
For some alternatives; currying:
or write your own capture-type (this is broadly comparable to what the compiler does for you when you use anon-methods / lambdas with captured variables, but has been implemented differently):
尝试backgroundWorker http://msdn.microsoft.com/en- us/library/system.componentmodel.backgroundworker.aspx 您可以使用 DoWorkEventArgs 将值传递给 DoWork 事件,并在 RunWorkerCompleted 中检索值。
try backgroundWorker http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx you can pass value to the DoWork event with DoWorkEventArgs and retrive value in the RunWorkerCompleted.
您应该能够使用匿名方法或 lambda 来提供完整的静态检查:
或者如果您想对结果执行某些操作:
但请注意,此“执行某些操作”仍然在新线程的上下文中运行(但可以访问外部方法(
Main
)中的其他变量由“捕获的变量”提供)。如果您有 C# 2.0(而不是更高版本),那么:
并且
You should be able to use an anonymous method or lambda to provide full static checking:
or if you want to do something with the result:
but note that this "do something" still runs in the context of the new thread (but with access to the other variables in the outer method (
Main
) courtesy of "captured variables").If you have C# 2.0 (and not above), then:
and