如何使用 lambda 表达式启动线程 c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9579491/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:58:45  来源:igfitidea点击:

How to start Thread with lambda expression c#

c#multithreading

提问by user979033

I have this class:

我有这门课:

public class Statistics
{
    List<string> _lsit;

    public List<string> ipList
    {
        get { return _lsit; }
        set { _lsit = value; }
    }

    string _Path = @"C:\Program Files\myApp.exe";
    string _Path = "";
    ProcessStartInfo ps = null;

    public getStatistics(string Path)
    {
        _Path = Path;
        getStatistics();
    }
}

I want to start the function Statistics with different Thead and i did somthing like:

我想用不同的 Thead 启动功能统计,我做了类似的事情:

Statistics stat = new Statistics (some path);
Thread<List<string>> lList = new Thread<List<string>>(() => tsharkIps.getStatistics());

but the compiler Error says "The non-generic type 'System.Threading.Thread' cannot be used with type arguments"

但编译器错误说“非通用类型'System.Threading.Thread'不能与类型参数一起使用”

I did not write all my class and only want to know hot to start the thread

我没有写我所有的课程,只想知道热启动线程

thanks

谢谢

采纳答案by Jon Skeet

You need to take a step back to start with and read the compiler error. Threadis not a generic type. It's really not at allclear what you're trying to do here, especially as you haven't even showna parameterless getStatistics()method (which should be called GetStatistics()to follow .NET naming conventions) and the parameterized getStatistics()method you haveshown doesn't have a return type.

您需要退后一步开始阅读编译器错误Thread不是泛型类型。这真的不是在所有清楚你想在这里做什么,尤其是当你还没有一个无参数getStatistics()方法(它应该被称为GetStatistics()遵循.NET命名约定)和参数化getStatistics()你的方法已经显示没有返回类型。

Starting a thread with a lambda expression is the easy part:

使用 lambda 表达式启动线程是最简单的部分:

Thread thread = new Thread(() => CallSomeMethodHere());
thread.Start();

It's not clear how that translates to your sample code though.

目前尚不清楚这如何转换为您的示例代码。

Or using the TPL in .NET 4, you can (and probably shoulduse Taskor Task<T>):

或者在 .NET 4 中使用 TPL,您可以(并且可能应该使用TaskTask<T>):

Task task = Task.Factory.StartNew(() => CallSomeMethodHere());

or

或者

Task<string> task = Task.Factory.StartNew(() => CallSomeMethodReturningString());

It's possible that you reallywant:

您可能真的想要:

Task<List<string>> statisticsTask = Task.Factory.StartNew(() => {
   Statistics statistics = new Statistics(path);
   return statistics.ipList();
});

Note that here the constructoris called within the new task - which is important, as it looks like that's probably doing all the work. (That's usually a bad idea to start with, but that's another matter.)

请注意,这里的构造函数在新任务中被调用 - 这很重要,因为看起来它可能正在完成所有工作。(这通常是一个坏主意,但这是另一回事。)

You should look at .NET naming conventionsin general, btw...

你应该看看一般的.NET命名约定,顺便说一句......