zoukankan      html  css  js  c++  java
  • C#开启线程的几种方式

    方式一:通过委托发起线程(BeginInvoke):

    Func<int, int> a = Test;
    IAsyncResult ar = a.BeginInvoke(20, OnCallBack, a);//倒数第二个参数是一个委托类型的参数,表示回调函数,当线程结束时会调用这个委托指向的方法;倒数第一个参数用来给回调函数传递数据;通过ar获取数据a

    方式二:通过Thread发起线程,thread创建的线程都是前台线程,线程池创建的线程都是后台线程

    1. thread参数为静态方法

            static void Downloadfile()
            {
                Console.WriteLine("开始下载" + Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(2000);
                Console.WriteLine("下载完成");
            }
    
            static void Main(string[] args)
            {
                Thread t = new Thread(Downloadfile);
                t.Start();
                Console.WriteLine("main");
                Console.ReadKey();
            }

    2. lamda表达式

    1 Thread t = new Thread(() =>
    2     {
    3         Console.WriteLine("开始下载" + Thread.CurrentThread.ManagedThreadId);
    4         Thread.Sleep(2000);
    5         Console.WriteLine("下载完成");
    6     });
    7 t.Start();    

    3. Thread参数为普通方法

     1     class Program
     2     {
     3         static void Downloadfile(object filename)
     4         {
     5             Console.WriteLine("开始下载" + filename+ Thread.CurrentThread.ManagedThreadId);
     6             Thread.Sleep(2000);
     7             Console.WriteLine("下载完成"+filename);
     8         }
     9 
    10         static void Main(string[] args)
    11         {
    12             //Thread t = new Thread(() =>
    13             //{
    14 
    15             //    Console.WriteLine("开始下载" + Thread.CurrentThread.ManagedThreadId);
    16             //    Thread.Sleep(2000);
    17             //    Console.WriteLine("下载完成");
    18             //});
    19             //Thread t = new Thread(Downloadfile);
    20             MyThread my = new MyThread("xxx.bt", "http://www.xxx.bbs");
    21             Thread t = new Thread(my.DownFile);
    22             t.Start();
    23             Console.WriteLine("main");
    24             Console.ReadKey();
    25         }
    26     }
    View Code
     1     class MyThread
     2     {
     3         private string filename;
     4         private string filepath;
     5         public MyThread(string fileName, string filePath)
     6         {
     7             this.filename = fileName;
     8             this.filepath = filePath;
     9         }
    10 
    11         public void DownFile()
    12         {
    13             Console.WriteLine("开始下载" + filepath + filename);
    14             Thread.Sleep(2000);
    15             Console.WriteLine("下载完成");
    16         }
    17 
    18     }
    19 }
    View Code

    方式三:线程池:适合用于小任务线程,

    WaitCallback:将方法排入队列以便执行,WaitCallback,表示要执行的方法。如果将方法成功排入队列,则为 true;否则为 false

     1     class Program
     2     {
     3         static void ThreadMethod(object state)
     4         {
     5             Console.WriteLine("线程开始"+Thread.CurrentThread.ManagedThreadId);
     6             Thread.Sleep(2000);
     7             Console.WriteLine("线程结束");
     8         }
     9         static void Main(string[] args)
    10         {
    11             ThreadPool.QueueUserWorkItem(ThreadMethod);
    12             ThreadPool.QueueUserWorkItem(ThreadMethod);
    13             ThreadPool.QueueUserWorkItem(ThreadMethod);
    14             ThreadPool.QueueUserWorkItem(ThreadMethod);
    15             ThreadPool.QueueUserWorkItem(ThreadMethod);
    16             Console.ReadKey();
    17         }
    18     }
    View Code

    方式四:任务线程

    1. 通过Task创建

     1 class Program
     2     {
     3         static void ThreadMethod()
     4         {
     5             Console.WriteLine("任务开始" + Thread.CurrentThread.ManagedThreadId);
     6             Thread.Sleep(2000);
     7             Console.WriteLine("任务结束");
     8         }
     9 
    10         static void Main(string[] args)
    11         {
    12             Task t = new Task(ThreadMethod);
    13             t.Start();
    14             Console.WriteLine("main");
    15             Console.ReadKey();
    16         }
    17     }
    View Code

    2. 通过TaskFactory创建

    TaskFactory tf = new TaskFactory();
    tf.StartNew(ThreadMethod);   

     tf.StartNew(ThreadMethod);
     tf.StartNew(ThreadMethod);

  • 相关阅读:
    PHP与CI学习笔记
    The Sum of the k-th Powers(Educational Codeforces Round 7F+拉格朗日插值法)
    Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)
    Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
    Gorgeous Sequence(HDU5360+线段树)
    Subsequence(HDU3530+单调队列)
    Qualification Rounds(Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)+状态压缩)
    Xenia and Weights(Codeforces Round #197 (Div. 2)+DP)
    字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛
    线段树优化建边
  • 原文地址:https://www.cnblogs.com/wxhao/p/13604924.html
Copyright © 2011-2022 走看看