设置首页收藏本站
开启左侧

C#中匿名线程通过Lambda表达式给线程传参数调用多线程

[复制链接]
15732670364 发表于 2022-5-1 10:14:27 | 显示全部楼层 |阅读模式
​线程方法定义:
  1. public void ThreadDo (string param,int n){   
  2.     //函数体实现处理方法
  3. }
复制代码
★匿名线程方法调用:
//匿名线程,Labmda表达式进行多参数函数传参
  1. Thread thread = new Thread(() => ThreadDo("param1",1));
  2. thread.IsBackground = true;
  3. thread.CheckIllegalCrossCtotorl = false;
  4. thread.Start();
复制代码
★总体概括介绍线程使用大体三种主流方法:
①这三种方法分别是:
1. 写一个类型,在构造函数中传参,而后在类型中写无参数函数,里面使用内部变量,从而达到传参的目的。
2. 使用lambda方法,通过直接调用已有的带参数函数,通过lambda表达式向线程传参。
3. 使用thread.start(object parameter)的方法。

第一种方式:
//通过ThreadStart委托告诉子线程执行什么方法
  1. ThreadStart threadStart = new ThreadStart(函数名);
  2. Thread thread = new Thread(threadStart);
  3. thread.Start();
复制代码
②C#如何在子线程中显示编辑控件内容,由于第一种线程调用无法传入参数所以在此利用委托Delegate给第一种线程调用传入参数
  1. delegate void add(string text);
  2. //开启委托
  3. public void Test(){
  4.     BeginInvoke(new add(Method), “待传入Method函数的参数”);
  5. }
  6. public void Method(string input){
  7.     //编辑框对象 textBox1
  8.      textBox1.Text = input;
  9. }
复制代码

二、按钮函数开启线程,在子线程中显示编辑框信息
  1. private void OpenImage_Click(object sender, EventArgs e) {
  2.       Thread thread = new Thread(new ThreadStart(Test));
  3.       thread.Start()
  4. }
复制代码
③用 ParameterizedThreadStart 委托实现:
ParameterizedThreadStart委托与ThreadStart委托非常相似,但ParameterizedThreadStart委托是面向带参数方法的。
  1. namespace ThreadApply{
  2.     public class Person
  3.     {
  4.         public string Name
  5.         {
  6.             get;
  7.             set;
  8.         }
  9.         public int Age
  10.        {
  11.             get;
  12.             set;
  13.         }
  14.     }
  15.     public class Message
  16.     {
  17.         public void ShowMessage(object person)
  18.         {
  19.             if (person != null)
  20.             {
  21.                 Person _person = (Person)person;
  22.                 string message = string.Format("\n{0}'s age is {1}!\nAsync threadId is:{2}",
  23.                     _person.Name, _person.Age, Thread.CurrentThread.ManagedThreadId);
  24.                 Console.WriteLine(message);
  25.             }
  26.             for (int n = 0; n < 10; n++)
  27.             {
  28.                 Thread.Sleep(300);
  29.                 Console.WriteLine("The number is:" + n.ToString());
  30.             }
  31.         }
  32.     }
  33.     class Program
  34.     {
  35.         static void Main(string[] args)
  36.         {
  37.             Console.WriteLine("Main threadId is:" + Thread.CurrentThread.ManagedThreadId);
  38.             Message message = new Message();
  39.             //绑定带参数的异步方法
  40.             Thread thread = new Thread(new ParameterizedThreadStart(message.ShowMessage));
  41.             Person person = new Person();
  42.             person.Name = "Jack";
  43.             person.Age = 21;
  44.             thread.Start(person);  //启动异步线程
  45.          }
  46.     }}
复制代码
详情请参考我的博客:https://blog.csdn.net/weixin_50016546/article/details/123954254
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表