using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace AutoResetEvent1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }    class Test
    {
        const int nNum = 100;
        static AutoResetEvent event_buy = new AutoResetEvent(false);
        static AutoResetEvent event_pay = new AutoResetEvent(false);        //Thread_Proc proc=new Thread_Proc();
        //创建两个线程
        Thread thread_buy = new Thread(new ThreadStart(Thread_Proc.BuyBook));
        thread_buy.Name ="Buy";//这里报错,提示“类、结构或接口成员声明中的标记“=”无效”
        Thread thread_pay = new Thread(new ThreadStart(Thread_Proc.PayMoney));
        
    }    class Thread_Proc
    {
        static public void PayMoney()
        {}        static public void BuyBook()
        {}
    }
}
为什么给线程取名会报这种错误啊

解决方案 »

  1.   

    我试了下,不用class Test这个类,直接在class Program中写的话不存在这种问题using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace AutoResetEvent1
    {
        class Program
        {
            static AutoResetEvent event_buy = new AutoResetEvent(false);
            static AutoResetEvent event_pay = new AutoResetEvent(false);
            const int nNum = 100;
            static void Main(string[] args)
            {
                //Thread_Proc proc=new Thread_Proc();
                //创建两个线程
                Thread thread_buy = new Thread(new ThreadStart(Thread_Proc.BuyBook));
                thread_buy.Name ="Buy";
                thread_buy.Start();        }
        }    class Thread_Proc
        {
            static public void PayMoney()
            {        }        static public void BuyBook()
            { }
        }
    }好奇怪啊
      

  2.   

    thread_buy.Name,这个不能直接放到变量声明区,放到一个方法中吧        //创建两个线程
            public void CreateThread()
            {
                Thread thread_buy = new Thread(new ThreadStart(Thread_Proc.BuyBook));
                thread_buy.Name = "Buy";//这里报错,提示“类、结构或接口成员声明中的标记“=”无效”[/color]
                Thread thread_pay = new Thread(new ThreadStart(Thread_Proc.PayMoney));
            }