我最近遇到了这样一段代码(这里是简化版,我检查过它并且有效):
using System;
using System.Threading;
namespace Rextester
{
public class Program
{
public class Foo
{
public Foo()
{
thread = new Thread(new ThreadStart(Loop));
}
private void Loop()
{
}
private Thread thread;
}
public static void Main(string[] args)
{
var foo = new Foo();
Console.WriteLine("How does it work??");
}
}
}
为什么这样的代码可以正常工作而没有编译器的任何提示?据我了解,线程应该从应该是静态的函数开始。 (或者无论如何都应该传递对对象和成员函数的引用)。但在这里我只看到传递对成员函数的引用。好像我错过了 C# 中的大事。可能存在传递 this
引用的隐式方式吗?
UPD:非常感谢。我只想补充一点这个事实。事实证明,编译器会自动处理委托(delegate)(传递正确的引用对象和方法)。这是 il 代码:
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 32 (0x20)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: nop
IL_0008: ldarg.0
IL_0009: ldarg.0
IL_000a: ldftn instance void Rextester.Program/Foo::Loop()
IL_0010: newobj instance void
[mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_0015: newobj instance void
[mscorlib]System.Threading.Thread::.ctor(class
[mscorlib]System.Threading.ThreadStart)
IL_001a: stfld class [mscorlib]System.Threading.Thread
Rextester.Program/Foo::thread
IL_001f: ret
} // end of method Foo::.ctor
最佳答案
But here I see only passing reference to member function.
文档对 ThreadStart 说它是一个委托(delegate)。它
"represents the method that executes on a Thread.
When a managed thread is created, the method that executes on the thread is represented by a ThreadStart delegate"
Thread should start from function which should be static.
这不一定是真的。该函数不必是静态的。
文档显示了这两个示例。使用静态方法和使用实例方法:
class Test
{
static void Main()
{
// To start a thread using a static thread procedure, use the
// class name and method name when you create the ThreadStart
// delegate. Beginning in version 2.0 of the .NET Framework,
// it is not necessary to create a delegate explicitly.
// Specify the name of the method in the Thread constructor,
// and the compiler selects the correct delegate. For example:
//
// Thread newThread = new Thread(Work.DoWork);
//
ThreadStart threadDelegate = new ThreadStart(Work.DoWork);
Thread newThread = new Thread(threadDelegate);
newThread.Start();
// To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ThreadStart delegate. Beginning in version
// 2.0 of the .NET Framework, the explicit delegate is not
// required.
//
Work w = new Work();
w.Data = 42;
threadDelegate = new ThreadStart(w.DoMoreWork);
newThread = new Thread(threadDelegate);
newThread.Start();
}
}
class Work
{
public static void DoWork()
{
Console.WriteLine("Static thread procedure.");
}
public int Data;
public void DoMoreWork()
{
Console.WriteLine("Instance thread procedure. Data={0}", Data);
}
}
编辑:
关于类型委托(delegate) documentation说:
A delegate is a type that represents references to methods with a particular parameter list and return type.
在此tutorial on MSDN on deleagates您可以看到如何使用 new
关键字实例化它:
Instantiating a delegate Once a delegate type has been declared, a delegate object must be created and associated with a particular method. Like all other objects, a new delegate object is created with a new expression.
这意味着由于 Loop
和 ThreadStart
具有相同的返回类型,即 void
和相同的参数列表,(此处为空)你可以使用实例方法 Loop
的名称实例化委托(delegate)。
编辑 2:
I just was confused about using member function without reference.
这是可行的,因为您在与声明方法的位置相同的类中声明了线程。
May be there is implicit way of passing
this
reference?
在这种情况下,答案是肯定的。如果您在类中调用方法或使用名称作为委托(delegate)(指向方法的指针),则 this
是隐式的。
关于c# - 使用成员函数启动线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46444268/