1.线程函数是类实例方法

类定义及方法实现:

public class ServerClass
{
    

    //类实例方法
    public void InstanceMethod()
    {
        Console.WriteLine("类实例方法");
        Thread.Sleep(3000);
        Console.WriteLine("类实例方法调用结束");
    }

    //类静态方法
    public static void StaticMethod()
    {
        Console.WriteLine("类静态方法");
        Thread.Sleep(5000);
        Console.WriteLine("类静态方法调用结束");
    }
}

在线程中使用类实例方法:

//实例化类对象
ServerClass serverObject = new ServerClass();
//创建线程并传入类实例方法
Thread InstanceCaller = new Thread(new ThreadStart(serverObject.InstanceMethod));
//启动线程
InstanceCaller.Start();

2.线程函数是类的静态方法

//创建线程并传入类静态方法
Thread StaticCaller = new Thread(new ThreadStart(ServerClass.StaticMethod));
StaticCaller.Start();//启动线程

3.如果需要阻塞主线程,添加下面语句:

//阻塞主线程,直到子线程执行完成
t.Join();

4.委托在线程中的使用

构造带委托的类:

//委托
public delegate void ExampleCallback(int lineCount);
public class ThreadWithState
{
    //成员变量
    private string boilerplate;
    private int numberValue;
    private ExampleCallback callback;

    //构造
    public ThreadWithState(string text, int number)
    {
        boilerplate = text;
        numberValue = number;
    }
    //增加委托的构造
    public ThreadWithState(string text, int number,
        ExampleCallback callbackDelegate) //构造时传入委托
    {
        boilerplate = text;
        numberValue = number;
        callback = callbackDelegate;
    }
    //成员
    public void ThreadProc()
    {
        Console.WriteLine(boilerplate, numberValue);
    }
}

构造类实例对象不带委托:

//实例化类对象
ThreadWithState tws = new ThreadWithState("子线程输出的内容: {0}.", 42);
//实例化线程对象
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
//启动线程
t.Start();

构造类实例对象带委托:

//带委托的构造
ThreadWithState tws1 = new ThreadWithState(
    "This report displays the number {0}.",
    42,
    new ExampleCallback(ResultCallback) //委托传入静态函数
);

//外部静态函数,供委托调用
static void ResultCallback(int lineCount)
{
    Console.WriteLine(
        "Independent task printed {0} lines.", lineCount);
}


//启动线程
Thread t1 = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
t.Join();//阻塞主线程

5.线程池使用

 

Console.WriteLine("C#线程池演示");

ThreadPool.QueueUserWorkItem(ThreadFunc);//创建任务队列
Console.WriteLine($"主线程:id->{Thread.CurrentThread.ManagedThreadId}");
Thread.Sleep(1000);//主线程休眠时,线程池会后台执行
//当主线程退出时,线程池也会退出,如果删除Thread.Sleep(1000);这句 ,那么ThreadFunc不会执行
Console.WriteLine("主线程程执行结束");


static void ThreadFunc(Object stateInfo)
{
    Console.WriteLine("线程ID:{0},{1},线程池输出信息",Thread.CurrentThread.ManagedThreadId,Thread.CurrentThread.IsBackground?"后台线程":"前台线程");
    Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread?"线程池":"普通线程");
}

演示效果:

C#多线程与线程池使用_主线程

完整示例源码:

Console.WriteLine("C#线程池演示");

ThreadPool.QueueUserWorkItem(ThreadFunc);//创建任务队列
Console.WriteLine($"主线程:id->{Thread.CurrentThread.ManagedThreadId}");
Thread.Sleep(1000);//主线程休眠时,线程池会后台执行
//当主线程退出时,线程池也会退出,如果删除Thread.Sleep(1000);这句 ,那么ThreadFunc不会执行
Console.WriteLine("主线程程执行结束");


static void ThreadFunc(Object stateInfo)
{
    Console.WriteLine("线程ID:{0},{1},线程池输出信息",Thread.CurrentThread.ManagedThreadId,Thread.CurrentThread.IsBackground?"后台线程":"前台线程");
    Console.WriteLine(Thread.CurrentThread.IsThreadPoolThread?"线程池":"普通线程");
}


//实例化类对象
ServerClass serverObject = new ServerClass();
//创建线程并传入类实例方法
Thread InstanceCaller = new Thread(new ThreadStart(serverObject.InstanceMethod));
//启动线程
InstanceCaller.Start();

Console.WriteLine("主线程");


//创建线程并传入类静态方法
Thread StaticCaller = new Thread(new ThreadStart(ServerClass.StaticMethod));
StaticCaller.Start();//启动线程

Console.WriteLine("主线程");

//实例化类对象
ThreadWithState tws = new ThreadWithState("子线程输出的内容: {0}.", 42);
//实例化线程对象
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
//启动线程
t.Start();
//在这里可执行主线程操作

//阻塞主线程,直到子线程执行完成
t.Join();
Console.WriteLine("子线线程已启动,并阻塞主线程");
Console.WriteLine("子线程执行完成后会输出");



//带委托的构造
ThreadWithState tws1 = new ThreadWithState(
    "带委托的线程输出 {0}.",
    42,
    new ExampleCallback(ResultCallback) //委托传入静态函数
);

//外部静态函数,供委托调用
static void ResultCallback(int lineCount)
{
    Console.WriteLine(
        "线程委托回调: {0} .", lineCount);
}

//启动线程
Thread t1 = new Thread(new ThreadStart(tws1.ThreadProc));
t1.Start();
Console.WriteLine("主线程被阻塞,等待子线程完成.");
t1.Join();//阻塞主线程
Console.WriteLine("子线程执行及委托任务完成,返回主线程");

public class ServerClass
{
    

    //类实例方法
    public void InstanceMethod()
    {
        Console.WriteLine("类实例方法");
        Thread.Sleep(3000);
        Console.WriteLine("类实例方法调用结束");
    }

    //类静态方法
    public static void StaticMethod()
    {
        Console.WriteLine("类静态方法");
        Thread.Sleep(5000);
        Console.WriteLine("类静态方法调用结束");
    }
}

//委托
public delegate void ExampleCallback(int lineCount);
public class ThreadWithState
{
    //成员变量
    private string boilerplate;
    private int numberValue;
    private ExampleCallback callback;

    //构造
    public ThreadWithState(string text, int number)
    {
        boilerplate = text;
        numberValue = number;
    }
    //增加委托的构造
    public ThreadWithState(string text, int number,
        ExampleCallback callbackDelegate) //构造时传入委托
    {
        boilerplate = text;
        numberValue = number;
        callback = callbackDelegate;
    }
    //成员
    public void ThreadProc()
    {
        Console.WriteLine(boilerplate, numberValue);
    }
}