实例讲解.NET多线程执行函数

这里为什么会出现多线程?原因是DebugLZQ在写一个LINQ综合Demo的时候遇到了多线程,便停下手来整理一下.关于多线程的文章,园子里很多很 多,因此关于多线程理论性的东西,LZ就不去多说了,这篇博文主要是用最简单的例子,总结下多线程调用函数的相关注意点,重点偏向应用和记忆.    1.多线程调用无参函数
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace 多线程 {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(“主线程开始”);
    Thread t = new Thread(new ThreadStart(ShowTime));//注意ThreadStart委托的定义形式
    t.Start();//线程开始,控制权返回Main线程
    Console.WriteLine(“主线程继续执行”);
    //while (t.IsAlive == true) ;
    Thread.Sleep(1000);
    t.Abort();
    t.Join();//阻塞Main线程,直到t终止
    Console.WriteLine(“--------------”);
    Console.ReadKey();
    }
    static void ShowTime()
    {
    while (true)
    {
    Console.WriteLine(DateTime.Now.ToString());
    }
    }
    }
    }
    注意ThreadStart委托的定义如下:

    可见其对传递进来的函数要求是:返回值void,无参数.
    2.多线程调用带参函数(两种方法)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace 多线程2_带参数 {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(“Main线程开始”);
    Thread t = new Thread(new ParameterizedThreadStart(DoSomething));//注意ParameterizedThreadStart委托的定义形式
    t.Start(new string[]{“Hello”,“World”});
    Console.WriteLine(“Main线程继续执行”);
    Thread.Sleep(1000);
    t.Abort();
    t.Join();//阻塞Main线程,直到t终止
    Console.ReadKey();
    }
    static void DoSomething(object  s)
    {
    string[] strs = s as string[];
    while (true)
    {
    Console.WriteLine(“{0}--{1}”,strs[0],strs[1]);
    }
    }
    }
    }
    注意ParameterizedThreadStart委托的定义如下:

    可见其对传入函数的要求是:返回值void,参数个数1,参数类型object
    using System;
    using System.Collections.Generic; u
    sing System.Linq;
    using System.Text;
    using System.Threading;
    namespace 多线程2_带参数2 {
    class Program
    {
    static void Main(string[] args)
    {
    Guest guest = new Guest()
    {
    Name=“Hello”, Age=99
    };
    Thread t = new Thread(new ThreadStart(guest.DoSomething));//注意ThreadStart委托的定义形式