c# 多线程 调用带参数函数

  • 线程操作主要用到Thread类,他是定义在System.Threading.dll下。使用时需要添加这一个引用。该类提供给我们四个重载的构造函数(以下引自msdn)。

  •  
  • Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。

  • Thread (ThreadStart) 初始化 Thread 类的新实例。

  • 由 .NET Compact Framework 支持。

  •  
  • Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。

  • Thread (ThreadStart, Int32) 初始化 Thread 类的新实例,指定线程的最大堆栈大小。

  • 由 .NET Compact Framework 支持。

  •  
  •  
  •  
  • 我们如果定义不带参数的线程,可以用ThreadStart,带一个参数的用ParameterizedThreadStart。带多个参数的用另外的方法,下面逐一讲述。

  •  
  • 一、不带参数的

  •  
  • using System;

  • using System.Collections.Generic;

  • using System.Text;

  • using System.Threading;

  •  
  • namespace AAAAAA

  • {

  • class AAA

  • {

  • public static void Main()

  • {

  • Thread t = new Thread(new ThreadStart(A));

  • t.Start();

  •  
  • Console.Read();

  • }

  •  
  • private static void A()

  • {

  • Console.WriteLine("Method A!");

  • }

  • }

  • }

  •  
  •  
  • 结果显示Method A!

  •  
  • 二、带一个参数的

  •  
  • 由于ParameterizedThreadStart要求参数类型必须为object,所以定义的方法B形参类型必须为object。

  •  
  • using System;

  • using System.Collections.Generic;

  • using System.Text;

  • using System.Threading;

  •  
  • namespace AAAAAA

  • {

  • class AAA

  • {

  • public static void Main()

  • {

  • Thread t = new Thread(new ParameterizedThreadStart(B));

  • t.Start("B");

  •  
  • Console.Read();

  • }

  •  
  • private static void B(object obj)

  • {

  • Console.WriteLine("Method {0}!",obj.ToString ());

  •  
  • }

  • }

  • }

  •  
  •  
  • 结果显示Method B!

  •  
  • 三、带多个参数的

  •  
  • 由于Thread默认只提供了这两种构造函数,如果需要传递多个参数,我们可以自己将参数作为类的属性。定义类的对象时候实例化这个属性,然后进行操作。

  •  
  • using System;

  • using System.Collections.Generic;

  • using System.Text;

  • using System.Threading;

  •  
  • namespace AAAAAA

  • {

  • class AAA

  • {

  • public static void Main()

  • {

  • My m = new My();

  • m.x = 2;

  • m.y = 3;

  •  
  • Thread t = new Thread(new ThreadStart(m.C));

  • t.Start();

  •  
  • Console.Read();

  • }

  • }

  •  
  • class My

  • {

  • public int x, y;

  •  
  • public void C()

  • {

  • Console.WriteLine("x={0},y={1}", this.x, this.y);

  • }

  • }

  • }

  •  
  •  
  • 结果显示x=2,y=3

  •  
  • 四、利用结构体给参数传值。

  •  
  • 定义公用的public struct,里面可以定义自己需要的参数,然后在需要添加线程的时候,可以定义结构体的实例。

  •  
  • //结构体

  • struct RowCol

  • {

  • public int row;

  • public int col;

  • };

  •  
  • //定义方法

  • public void Output(Object rc)

  • {

  • RowCol rowCol = (RowCol)rc;

  • for (int i = 0; i < rowCol.row; i++)

  • {

  • for (int j = 0; j < rowCol.col; j++)

  • Console.Write("{0} ", _char);

  • Console.Write("\n");

  • }

  • }

  • --------------------- 本文来自 wangzh300 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/wangzh300/article/details/6969354?utm_source=copy
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用Thread类来创建多个线程,并通过调用方法来启动这些线程。如果要在多线程中调用参数有返回值的方法,可以使用委托和异步回调来实现。下面是一个简单的示例代码,演示如何创建和启动多个线程来调用同一个参数有返回值的方法: ```csharp using System; using System.Threading; class Program { static void Main(string[] args) { // 创建3个线程 Thread t1 = new Thread(new ParameterizedThreadStart(MyMethod)); Thread t2 = new Thread(new ParameterizedThreadStart(MyMethod)); Thread t3 = new Thread(new ParameterizedThreadStart(MyMethod)); // 启动这些线程 t1.Start(1); t2.Start(2); t3.Start(3); // 等待这些线程结束 t1.Join(); t2.Join(); t3.Join(); Console.WriteLine("所有线程已结束"); } static void MyMethod(object param) { int num = (int)param; // 这里是你要执行的方法 Console.WriteLine("线程 {0} 正在执行 MyMethod,参数为 {1}", Thread.CurrentThread.ManagedThreadId, num); // 模拟方法执行 Thread.Sleep(1000); // 返回结果 int result = num * 2; // 调用回调函数返回结果 AsyncCallback callback = new AsyncCallback(MyCallback); callback.BeginInvoke(result, null, null); } static void MyCallback(IAsyncResult result) { int num = (int)result.AsyncState; Console.WriteLine("线程 {0} 的 MyMethod 方法返回了结果 {1}", Thread.CurrentThread.ManagedThreadId, num); } } ``` 在这个示例中,我们创建了3个线程,并通过调用Thread.Start()方法来启动它们。这些线程都会调用同一个方法MyMethod(),并传入一个整数参数。在MyMethod()方法中,我们模拟了方法的执行过程,并最终返回了一个整数结果。为了返回结果,我们使用了异步回调的方式,将结果传递给MyCallback()方法进行处理。在MyCallback()方法中,我们输出了方法执行的结果。 需要注意的是,多线程编程需要注意线程安全性,以免出现竞态条件等问题。在实际开发中,需要根据具体情况来选择适合的多线程编程模型和技术。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值