【问题标题】:cannot be accessed with an instance reference; qualify it with a type name instead无法通过实例引用访问;改为使用类型名称来限定它
【发布时间】:2012-11-24 20:36:47
【问题描述】:

使用示例 1:在此 MSDN tutorial 上创建、启动和在线程之间交互,更具体地说是 Main() 中的第 3 行到第 7 行

我有以下代码,但出现以下错误:

不能通过实例引用访问;用类型限定它 取而代之。

Program.cs

public static ThreadTest threadTest = new ThreadTest();
private static Thread testingThread = new Thread(new ThreadStart(threadTest.testThread()));
static void Main(string[] args)
{

}

ThreadTest.cs

public static void testThread()
{
}

【问题讨论】:

标签: c# multithreading


【解决方案1】:

您的testThread 是一个静态方法,因此可以通过类型名称获得。因此,不要使用 istance threadTest,而是使用 ThreadTest 类型。

// public static void testThread()
testingThread = new Thread(new ThreadStart(ThreadTest.testThread));

或者更改方法声明(去掉static):

// public void testThread()
testingThread = new Thread(new ThreadStart(threadTest.testThread));

您还应该将方法传递给委托ThreadTest.testThread(括号已删除),而不是传递方法调用的结果ThreadTest.testThread()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多