.NET - 有什么办法来创建一个非静态线程的方法? [英] .NET - Is there any way to create a non-static thread method?

查看:211
本文介绍了.NET - 有什么办法来创建一个非静态线程的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以在创建.NET非静态线程的方法? 让我看看code吧。

下面code不工作:

的ThreadStart TS =委托{drawFloorAround(); };

公共无效drawFloorAround()
{
            ...
}

给出了这样的错误 - >字段初始不能引用非静态字段,方法或属性。 如果我改变了方法做静态的,它的工作原理。但我不想。

解决方案
  

...给出了这样的错误一个字段初始不能引用非静态字段,方法或属性。

更仔细阅读错误消息。据pcisely告诉你$ P $什么是错的。 A 字段初始值的不能引用的非静态方法的。这是因为编译器正试图保护你不受此错误:

  C类
{
    INT FOO;
    INT栏= GetBar();
    市民C(INT newFoo)
    {
        this.foo = newFoo;
    }
    私人诠释GetBar(){返回this.foo + 1; }
}
 

您做新的C(123)。什么是条设置为?如果这是法律code,那么它将被设置为1,而不是124。为什么呢?因为首先FOO被初始化为零,然后GetBar()被调用,然后构造体设置this.foo至123

要prevent这个错误简直是非法引用实例方法或字段的字段初始值。

现在,你很可能会指出,在code,你不这样做的使用的实例方法,你只的引用的吧。你从来没有真正的通话的吧。这实际上是安全的。然而,C#的规则,设计简单和保守;即使我们可以证明这种情况下是安全的,我们采取了保守的,简单的路径,说的任意的参考实例字段初始化是非法的。

  

如果我的方法更改为静态的,它的工作原理。

正确的。在这种情况下,该方法不依赖于哪个尚未建立实例状态

  

但我不想。

确定,那么你唯一的其他选择是停止使用字段初始值。将在构造函数初始化;那么你负起责任,确保初始化不小心使用未初始化的状态。

Is there any way to create a non-static thread method at .NET? Show me code please.

The following code doesn't work:

ThreadStart ts = delegate { drawFloorAround(); };

public void drawFloorAround()
{
            ...
}

Gives this error -> "A field initializer cannot reference the non-static field, method, or property". If I change the method do static, it works. But I don't want to.

解决方案

... gives this error "A field initializer cannot reference the non-static field, method, or property".

Read the error message more carefully. It is telling you precisely what is wrong. A field initializer cannot reference a non-static method. That's because the compiler is trying to protect you from this bug:

class C
{
    int foo;
    int bar = GetBar();
    public C(int newFoo)
    {
        this.foo = newFoo;
    }
    private int GetBar() { return this.foo + 1; }
}

You do "new C(123)". What is bar set to? If this were legal code then it would be set to 1, not 124. Why? Because first foo gets initialized to zero, then GetBar() gets called, then the constructor body sets this.foo to 123.

To prevent this bug it is simply illegal to reference an instance method or field in a field initializer.

Now, you might reasonably point out that in your code, you do not use the instance method, you only reference it. You never actually call it. This actually is safe. However, the rules of C# are designed to be simple and conservative; even though we could prove that this case is safe, we take the conservative, simple path and say that any reference to the instance in a field initializer is illegal.

If I change the method to static, it works.

Correct. In that case, the method does not depend upon instance state which has not yet been set up.

But I don't want to.

OK, then your only other choice is to stop using a field initializer. Put the initialization in the constructor; you then take responsibility for ensuring that the initialization does not accidentally use uninitialized state.

这篇关于.NET - 有什么办法来创建一个非静态线程的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆