【问题标题】:"System.threading.threadStart' is a 'type', which is not valid in the given context'? (C#)“System.threading.threadStart'是一个'类型',在给定的上下文中无效'?(C#)
【发布时间】:2012-12-16 12:51:36
【问题描述】:

我在var ts = System.Threading.ThreadStart(delegate() 上收到错误消息(红线已淹没在System.Threading.ThreadStart 下)。问题是什么?

using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public Program()
        {
            int[] iArray = new int[3];
            iArray[0] = 2;
            iArray[1] = 1;
            iArray[2] = 5;
            var ts = System.Threading.ThreadStart(delegate()
            {
                foreach (int i in iArray)
                    Foo(i);
            });
        }

        public void Foo(int i)
        {
            Console.WriteLine(i + ",");
        }

        public static void Main(String[] args)
        {
            Program p = new Program();
        }
    }
}

【问题讨论】:

    标签: c# .net multithreading delegates lambda


    【解决方案1】:

    您缺少new

    var ts = new System.Threading.ThreadStart(delegate()
             {
                 foreach (int i in iArray)
                     Foo(i);
             });
    

    顺便说一句:

    您不必在 ThreadStart 前面加上其命名空间 System.Threading,因为您的 *.cs 文件顶部已经有一个 using 声明。

    【讨论】:

    • 嗯,.. 我不知道是该感谢你解决了这个问题,还是只是生你的气,因为你让我看到了我是多么愚蠢。但我只是在开玩笑。我应该多注意一点。谢谢你! :)
    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 2018-05-07
    • 2020-04-16
    • 2021-01-19
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多