加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用成员函数启动线程

发布时间:2020-12-15 23:40:52 所属栏目:百科 来源:网络整理
导读:我最近遇到过这样的代码(这里是简化版,我检查了它并且它有效): using System;using System.Threading;namespace Rextester{ public class Program { public class Foo { public Foo() { thread = new Thread(new ThreadStart(Loop)); } private void Loop()
我最近遇到过这样的代码(这里是简化版,我检查了它并且它有效):

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??");
        }
    }
}

为什么这些代码在没有编译器抱怨的情况下工作?据我所知,Thread应该从应该是静态的函数开始. (无论如何都应该传递对象和成员函数的引用).但在这里我看到只传递成员函数的引用.好像我错过了c#中的大事.可能有隐含的方式传递此引用?

UPD:非常感谢.我只是想补充一点这个事实.事实证明,编译器会自动处理委托(传递正确的引用对象和方法).这是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说它是代表.它

“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); 
    }
}

编辑:

关于类型代表,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的名称来设置委托.

编辑2:

I just was confused about using member function without reference.

这是有效的,因为您声明了与声明方法的类相同的类中的线程.

May be there is implicit way of passing this reference?

在这种情况下,答案是肯定的.如果在类中调用方法或使用名称作为委托(指向方法的指针),那么这是隐式的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读