c#线程实例复习(一)

<pre name="code" class="csharp">一下创建都是后台线程
1.通过委托的方式执行线程
using System;
using System.Threading;

internal class Test
{
    private static void show()
    {
        int i = 0;
        for (; i < 200; i++)
         Console.WriteLine("显示结果"+i);
        
    }

    public static void Main()
    {
        Action a = show;
        a.BeginInvoke(null,null);   //开启一个线程,这个方法可以不用等待线程结束而程序结束
	//第二个参数是一个委托类型表示一个回调函数,就是当线程结束的时候会调用这个方法
        Console.WriteLine("主函数窗口");  //主线程中的代码
        
    }
}

2.得到线程的返回值并获取线程是否结束(方法一)
using System;

internal class Test
{
    private static int show(int w)
    {
        Console.WriteLine("子线程开启");
        return w + 100;

    }

    public static void Main()
    {
        Func<int, int> a = show;
        IAsyncResult res = a.BeginInvoke(100,null, null);  //开启一个线程并使用一个接口来接受状态
        Console.WriteLine("主线程开启");
        while (res.IsCompleted == false)   //获取线程的状态
        {
            Console.WriteLine(".");
        }
        int myres = a.EndInvoke(res);    //通过EndInvoke方法来获得返回值 
        Console.Write("线程结束");
        Console.ReadLine();

    }
}

3.获取线程是否结束(方法二)
using System;
using System.Threading;

internal class Test
{
    private static int show(int w)
    {
        Console.WriteLine("子线程开启");
        Thread.Sleep(200);
        return w + 100;


    }

    public static void Main()
    {
        Func<int, int> a = show;
        IAsyncResult res = a.BeginInvoke(100,null,null);
        bool isend = res.AsyncWaitHandle.WaitOne(100);  //100表示一个超时的时间如果100毫秒以内线程结束返回true,否则返回false
        if (isend == true)
        {
            Console.WriteLine("线程在100毫秒以内结束了");
        }
        else
        {
            Console.WriteLine("线程在100毫秒以内没有结束");

        }
    }
}

3.获取线程是否结束
using System;

internal class Test
{
    private static int show(int w)
    {
        return w + 2;
    }

    public static void Main()
    {
        Func<int, int> a = show;
        IAsyncResult res = a.BeginInvoke(100, Oncallback, null);  //使用回调函数来进行判断线程结束
        Console.WriteLine("主线程结束");
        Console.ReadKey();
    }

    private static void Oncallback(IAsyncResult ar)
    {
        Console.WriteLine("子线程结束");
    }
}

4.回调函数的使用
using System;
using System.Text;
using System.Threading;

internal class Test
{
    private static string  show(string str)   //线程函数
    {
        Console.WriteLine("show");
        string str1 = "jieshula";
        return str1;
    }

    private static void OncallBack(IAsyncResult res)  //回调函数
    {
        Func<string, string> a = res.AsyncState as Func<string, string>;  //将类型转换
        string str = a.EndInvoke(res);   //得到线程结果
        Console.WriteLine(str);
        Console.WriteLine("回调函数调用");
    }

    public static void Main()
    {
        Func<string, string> a = show;
        IAsyncResult res = a.BeginInvoke("kaishila", OncallBack, a);  //a作为回调函数的参数
        //在回调函数中得到线程的返回结果
        Console.WriteLine("主线程结束");
        Thread.Sleep(1000);
    }
}

5.将回调函数写成
using System;
using System.Threading;

internal class Test
{
    private static int sum(int a)
    {
        return a*100;
    }

    static void OnCall(IAsyncResult res)
    {
        Func<int, int> a = res.AsyncState as Func<int, int>;
        int jieguo = a.EndInvoke(res);
        Console.WriteLine(jieguo);
        Console.WriteLine("回调结束");
    }

    public static  void Main()
    {
        Func<int, int> a = sum;
        a.BeginInvoke(100, ar =>
        {
            int result = a.EndInvoke(ar);
            Console.WriteLine(result);
        },null);
        Thread.Sleep(100);
    }

}



以上创建的都是后台线程,一下创建的都是前台线程
5.通过Thread对象来创建线程
using System;
using System.Threading;

internal class Test
{
    private static void DownLoad()   //线程函数
    {
        Console.WriteLine("开始下载文件");
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId);   //获取线程ID
        Console.WriteLine("文件下下载完成");
    }

    public static  void Main()
    {
        Thread thread=new Thread(DownLoad);
        thread.Start();
        //Action a = DownLoad;
        //a.BeginInvoke(null, null);
        Console.WriteLine("mainxiancheng");
        Console.ReadLine();

    }
}

6.使用lambda表达式创建线程体
using System;
using System.Threading;

internal class Test
{
    private static void DownLoad()   //线程函数
    {
        Console.WriteLine("开始下载文件");
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId);   //获取线程ID
        Console.WriteLine("文件下下载完成");
    }

    public static  void Main()
    {
        Thread thread = new Thread(() =>
        {
            Console.WriteLine("开始下载");
            for(int i=0;i<100;i++)
                Console.WriteLine(i);
            Console.WriteLine("下载完成");
        });
        thread.Start();
        Console.WriteLine("主线程结束");

    }
}

7.线程中传递参数
using System;
using System.Threading;

class Test
{
    private static void show(object str)
    {
        Console.WriteLine("下载的文件名:"+str);
    }

    public static void Main()
    {
        Thread thread=new Thread(show);
        thread.Start("我的家");

    }
}

8.其他例子
using System;
using System.Threading;

internal class myThread
{
    private string name;
    private int num;

    public myThread(string name, int num)
    {
        this.name = name;
        this.num = num;
    }

    public void show()
    {
        Console.WriteLine("开始下载文件");
        Console.WriteLine(name+" "+num);
        Thread.Sleep(100);
        Console.WriteLine("文件下载完成");
    }
}

internal class Test
{

    public static void Main()
    {
       myThread myfile=new myThread("我的家",10);
       Thread thread=new Thread(myfile.show);   //线程构造参数不仅可以是静态函数也可以是普通函数
        thread.Start();
	//thread.Join();   //表示让当前thread线程执行完成后才能继续执行主线程中的代码
	Console.WriteLine("开始执行主线程的代码");
    }
}


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值