ThreadStart委托类型

1.创建ThreadStart委托类型对象

namespace Test
{
    class TestThread
    {
        private static void ThreadFuncOne()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + "   i =  " + i);
            }
            Console.WriteLine(Thread.CurrentThread.Name + " has finished");
        }

        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "MainThread";

            // public delegate void ThreadStart();声明一个ThreadStart委托类型
            //如下代码使用TestThread.ThreadFuncOne方法创建一个委托对象 new ThreadStart(TestThread.ThreadFuncOne)
            Thread newThread = new Thread(new ThreadStart(TestThread.ThreadFuncOne));
            newThread.Name = "NewThread";

            for (int j = 0; j < 20; j++)
            {
                if (j == 10)
                {
                    newThread.Start();
                    newThread.Join();
                }
                else
                {
                    Console.WriteLine(Thread.CurrentThread.Name + "   j =  " + j);
                }
            }
            Console.Read();
        }
    }
}

 

2.Thread类有4个重载:
    // 摘要:
    //     创建并控制线程,设置其优先级并获取其状态。
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(_Thread))]
    [ComVisible(true)]
    public sealed class Thread : CriticalFinalizerObject, _Thread
    {
        //
        // 摘要:
        //     初始化 System.Threading.Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。
        //
        // 参数:
        //   start:
        //     System.Threading.ParameterizedThreadStart 委托,它表示此线程开始执行时要调用的方法。
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     start 为 null。
        [SecuritySafeCritical]
        public Thread(ParameterizedThreadStart start);
        //
        // 摘要:
        //     初始化 System.Threading.Thread 类的新实例。
        //
        // 参数:
        //   start:
        //     System.Threading.ThreadStart 委托,它表示此线程开始执行时要调用的方法。
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     start 参数为 null。
        [SecuritySafeCritical]
        public Thread(ThreadStart start);
        //
        // 摘要:
        //     初始化 System.Threading.Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。
        //
        // 参数:
        //   start:
        //     System.Threading.ParameterizedThreadStart 委托,它表示此线程开始执行时要调用的方法。
        //
        //   maxStackSize:
        //     线程要使用的最大堆栈大小(以字节为单位);如果为 0 则使用可执行文件的文件头中指定的默认最大堆栈大小。重要地,对于部分受信任的代码,如果 maxStackSize
        //     大于默认堆栈大小,则将其忽略。不引发异常。
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     start 为 null。
        //
        //   T:System.ArgumentOutOfRangeException:
        //     maxStackSize 小于零。
        [SecuritySafeCritical]
        public Thread(ParameterizedThreadStart start, int maxStackSize);
        //
        // 摘要:
        //     初始化 System.Threading.Thread 类的新实例,指定线程的最大堆栈大小。
        //
        // 参数:
        //   start:
        //     System.Threading.ThreadStart 委托,它表示此线程开始执行时要调用的方法。
        //
        //   maxStackSize:
        //     线程要使用的最大堆栈大小(以字节为单位);如果为 0 则使用可执行文件的文件头中指定的默认最大堆栈大小。重要地,对于部分受信任的代码,如果 maxStackSize
        //     大于默认堆栈大小,则将其忽略。不引发异常。
        //
        // 异常:
        //   T:System.ArgumentNullException:
        //     start 为 null。
        //
        //   T:System.ArgumentOutOfRangeException:
        //     maxStackSize 小于零。
        [SecuritySafeCritical]
        public Thread(ThreadStart start, int maxStackSize);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值