C#——线程基础

来源:互联网 发布:无法触碰影评 知乎 编辑:程序博客网 时间:2024/06/05 04:40

时间:2016.10.31

介绍线程相关的基础基础部分


1. 基础知识

C#中的Thread类

public sealed class Thread : CriticalFinalizerObject, _Thread{    // 四个基础构造函数    public Thread( ThreadStart start );    public Thread( ParameterizedThreadStart start );    public Thread( ThreadStart start, int maxStacksize );    public Thread( ParameterizedThreadStart start, int maxStackSize );    //...}

 
其中,线程函数可以是两种类型

    [ComVisible(true)]    public delegate void ThreadStart();    [ComVisible(false)]    public delegate void ParameterizedThreadStart(object obj);

2. 最简单的示例

在命令控制台项目中,创建一个最简单的线程并启动线程

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace ThreadTest{    static class ThreadTest    {        static void main(string[] args)        {            new Thread( ThreadProc ).Start();        }        static private void ThreadProc()        {            int count = 0;            while( true )            {                Console.WriteLine( " current count is: {0}", count );                count++;                Thread.Sleep( 1000 );            }        }           }   }

【返回】

C#分类目录

0 0
原创粉丝点击