Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 参数化线程,理解lambda表达式_C#_Multithreading_Lambda - Fatal编程技术网

C# 参数化线程,理解lambda表达式

C# 参数化线程,理解lambda表达式,c#,multithreading,lambda,C#,Multithreading,Lambda,我阅读了下面的代码,并想进一步了解lambda表达式 ThreadStart starter = () => Threads.QueryThread(tmpweb, LastExecutedate); var thread = new Thread(starter); thread.Start(); 读取后,不会给出任何参数(()=>),表达式是使用静态方法Threads.QueryThread的匿名委托初始化ThreadStart实例?是这样吗 使用静态方法Threads.QueryT

我阅读了下面的代码,并想进一步了解lambda表达式

ThreadStart starter = () => Threads.QueryThread(tmpweb, LastExecutedate);
var thread = new Thread(starter);
thread.Start();
读取后,不会给出任何参数(
()=>
),表达式是使用静态方法Threads.QueryThread的匿名委托初始化ThreadStart实例?是这样吗

使用静态方法Threads.QueryThread的匿名委托?是这样吗

不完全是,;它是指由匿名方法实现的方法目标的委托实例 依次调用
Threads.QueryThread
方法,可能使用提供对
tmpweb
LastExecutedate
访问的捕获上下文(可能通过
this
?)

i、 e

以及:


注意,根据
tmpweb
LastExecutedate
的具体内容,这里有一些差异的范围-如果它们实际上不涉及任何捕获的上下文(所有内容都是静态的,或者所有内容都在
this
),然后编译器可以做一些稍微不同的事情作为优化。

为什么不使用
Task.Run
?@PanagiotisKanavos在
Task.Run
Thread.Start
之间进行选择有很多原因-这真的不像“使用x”或“使用y”那么简单;我可以反过来问“为什么要使用
Task.Run
?”——这两个问题都是完全合理的,都需要更多的上下文来了解正在发生的事情internally@MarcGravell当问题是Lambdas如何工作时,很可能是默认情况下使用了线程,或在遵循旧教程后,由于.NET版本的原因,阿里无法使用Task.Run。不过,我对每种方法的优点和差异感兴趣,以便以后学习如何做更好的选择。是的,tmpweb是一个局部变量,最后执行了一个私有属性,显示的代码来自同一类中的一个方法
class SomeClass {
    public SomeType tmpweb;
    public SomeOtherType @this;
    public void TheMethod() {
        Threads.QueryThread(tmpweb, @this.LastExecutedate);
    }
}
SomeClass ctx = new SomeClass { @this = this };
...
ctx.tmpweb = ... 
...
ThreadStart starter = new ThreadStart(ctx.TheMethod);
var thread = new Thread(starter);
thread.Start();