C# 多线程学习 六: 向线程传递数据和异常处理

向线程传递数据:

  1. 如果你想往线程的启动方法中传递参数,最简单的办法是使用lambda表达式,在里边使用参数调用方法。
  2. 在C#3.0之前没有lambda 表达式,可以使用Thread的Stat方法来传递参数。

For text One

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

namespace ConsolePlan
{
    class Program
    {
        private const string MainThreadName = "MainThread";
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = MainThreadName;

            Program pr = new Program();
            Thread thread = new Thread(() => Pro("bala bala bala...."));
            thread.Name = "SubThread";
            thread.Start();

 
            Console.ReadKey(true);
        }
        static void Pro(string showStr)
        {
            Console.WriteLine(showStr);
        }
       
    }
}

For Text Two

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

namespace ConsolePlan
{
    class Program
    {
        private const string MainThreadName = "MainThread";
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = MainThreadName;

            Program pr = new Program();
            Thread thread = new Thread(Pro);
            thread.Name = "SubThread";
            thread.Start("la la la ....");

 
            Console.ReadKey(true);
        }
        static void Pro(object showStr)
        {
            Console.WriteLine(showStr);
        }
       
    }
}

异常处理 try catch

  1. 创建线程时候在作用范围内的try/ catch ,在线程开始执行就与线程没有关系了,(人话就是线程间不能相互捕获异常,各自捕获各自的)
  2. 在WPF 和WinFrm中, 可以订阅全局异常处理实践。
  3. 如果想捕获全局异常可以注册事件:AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);在方法中处理异常机制问题

For Text

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

namespace ConsolePlan
{
    class Program
    {
        private const string MainThreadName = "MainThread";
        private readonly static object lockObj = new object();
        static void Main(string[] args)
        {
            Thread.CurrentThread.Name = MainThreadName;

            Program pr = new Program();
            Thread thread = new Thread(Pro);
            thread.Name = "SubThread";
            thread.Start("la la la ....");
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Console.ReadKey(true);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var exc = (Exception)e.ExceptionObject;
            Debug.Print(exc.StackTrace + "\t" + exc.Message);
        }
        static void Pro(object showStr)
        {
            Console.WriteLine(showStr);
            throw new Exception("Mesage Error!");
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值