C#之线程

今天给大家分享关于C#的几种调用的方法。

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

namespace _006线程池
{
    class Program
    {
        static void Main(string[] args)
        {
            // Thread t1 = new Thread(ThreadMain) { IsBackground = false };//为true的时候就需要结束该线程,不管你是否结束,false的时候是一个前台线程,他会等你结束后在结束
            //  t1.Abort();//终止线程,抛出异常
            // t1.Start();
            // t1.Join();//阻止线程运行,知道实例线程结束

            //Console.WriteLine("Thread +" + Thread.CurrentThread.ManagedThreadId + " started");
            //Console.WriteLine("Main thread ending now.");
            ThreadPool.QueueUserWorkItem(ThreadMain);//线程池预留空线程等待发配任务,而发配任务的时候,随机给完成任务的线程重新发配任务
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            ThreadPool.QueueUserWorkItem(ThreadMain);
            Console.ReadKey();


        }
       
        static void ThreadMain(object state)
        {
            Console.WriteLine("Thread +" + Thread.CurrentThread.ManagedThreadId + " started");

            Thread.Sleep(1000);
           
        }     }
}

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

namespace _007线程_任务开启
{
    class Program
    {
        static void Main(string[] args)
        {

            //第一种方式
            //Task t = new Task(ThreadMain);//传递一个线程执行一个方法
            //t.Start();//用任务开启一个线程

            //第二种方式开启的方式
            TaskFactory task = new TaskFactory();
            Task t=  task.StartNew(ThreadMain);

            Console.ReadKey();
        }
        static void ThreadMain()
        {
            Console.WriteLine("Thread +" + Thread.CurrentThread.ManagedThreadId + " started");

            Thread.Sleep(1000);

        }
    }
}
--------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _004线程_委托发起的线程
{
    class Program
    {
        static void Main(string[] args)//开启一个线程,从上到下的执行这些内容,其实就是一个进程
        {
            //第一种开启线程的方式
            //Func<int,string,int> a = Text;
            ////a.BeginInvoke(15,"萌萌",null,null);//开启一个线程,执行a所引用的方法,需要一些时间去操作,他的线程与main的线程是异步操作
            //IAsyncResult ar= a.BeginInvoke(15, "萌萌", null, null);//他的返回值是当前异步线程的状态,return 的返回值是在线程结束时返回的
            //while (ar.IsCompleted==false)
            //{
            //    Thread.Sleep(10);
            //    Console.WriteLine("*");
            //}
            //int res = a.EndInvoke(ar);//在线程结束时拿到返回值100
            //Console.WriteLine(res);
            //Console.WriteLine("main");
            //Console.ReadKey();//运行新的线程的时候必须结束暂停

            //第二种开启线程的方式
            //Func<int, string, int> a = Text;
            //IAsyncResult ar = a.BeginInvoke(15, "萌萌", null, null);//判断线程运行的状态
            //bool IsEnd=  ar.AsyncWaitHandle.WaitOne(1000);//等待线程结束时间间隔
            //if (IsEnd)
            //{
            //    int res = a.EndInvoke(ar);
            //    Console.WriteLine(res);
            //}

            //第三种发起线程的方式
            Func<int, string, int> a = Text;
            /* IAsyncResult ar=  a.BeginInvoke(15, "萌萌", BackOut, a);/*///倒数第二个参数是回调函数,倒数第一个参数是委托对象(BackOut回调函数是在线程结束时调用)
            a.BeginInvoke(15, "萌萌", ar =>//用lambad表达式去检测结束一个线程
            {
                int res = a.EndInvoke(ar);
                Console.WriteLine(res);
            }, a);
            Console.ReadKey();





        }
        static void BackOut(IAsyncResult ar)//传递线程状态
        {
            Func<int, string, int> a = ar.AsyncState as Func<int, string, int>;
            int res = a.EndInvoke(ar);
            Console.WriteLine(res);

        }
        static int Text(int id, string name)
        {
            Console.WriteLine("测试线程" + id + name);
            Thread.Sleep(200);//单位为ms,线程睡眠0.2s
            return 100;
        }
    }
}

  

原文地址:https://www.cnblogs.com/shuanglu/p/8297561.html