C# 往线程里传参数的方法总结

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");
  }
  }

原文地址:https://www.cnblogs.com/Liyuting/p/9087216.html

时间: 2024-08-28 18:27:19

C# 往线程里传参数的方法总结的相关文章

C#,往线程里传参数的方法总结

C#,往线程里传参数的方法总结 Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初始化 Thread 类的新实例.  由 .NET Compact Framework 支持.  Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈

VC6.0中创建的线程的传参数问题

VC中的win32控制台程序,然后包含MFC的程序,用CreateThread()向其对应函数传参数的问题 // test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "test.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FIL

用线程执行带参数的方法

最近的项目用到了线程,在线程里面需要执行带有参数的方法.之前在百度里面也查到了好多方法,今天想把自己在网上查询结果和自己的应用情况做一个记录,方便后续用到时候查看. 具体需求是这样的:有一个开锁的动作,这个锁所在的PLC地址位需要保持1的状态(开锁状态)维持15秒,然后从1——>0. 我还是个小菜鸟,有写的不对的地方,还请大侠们多多指教. private void btn_OpenLeftLock_Click(object sender, RoutedEventArgs e) { try { T

jquery绑定事件时如何向事件函数里传参数

举例子说明: 步骤1:var button=$('<button type="button" class="btn btn-default">提交</button>'); button.bind("click",{menuid:"01"},form_submit); 如上代码所示即是button按钮绑定了click事件,而其对应的函数为form_submit();并传递了参数menuid,其值为01,

二维数组的传参数的方法

如何将二维数组作为函数的参数传递 今天写程序的时候要用到二维数组作参数传给一个函数,我发现将二维数组作参数进行传递还不是想象得那么简单里,但是最后我也解决了遇到的问题,所以这篇文章主要介绍如何处理二维数组当作参数传递的情况,希望大家不至于再在这上面浪费时间. 正文: 首先,我引用了谭浩强先生编著的<C程序设计>上面的一节原文,它简要介绍了如何 将二维数组作为参数传递,原文如下(略有改变,请原谅): [原文开始] 可以用二维数组名作为实参或者形参,在被调用函数中对形参数组定义时可以指定所有维数的

Linux线程体传递参数的方法详解

传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thread; void * fn(void *arg) { int i = *(int *)arg; cout<<"i = "<<i<<endl; return ((void *)0); } int main() { int err1; int i=10; e

Spring MVC前台POST/GET方式传参数的方法

假设前台通过submit传值,代码如下: <form action="testPost.do" method="post"> 页码:<input type="text" name="page" /><br /> 每页容量:<input type="text" name="num" /><br /> <input type=

ParameterizedThreadStart,ThreadStart的使用,线程Thread传参数

Thread threadWithParam = new Thread(new ParameterizedThreadStart(new ThreadTest().ShowMsg));//threadWithParam.Start("this is a param."); threadWithParam.Start( thread.Start(); "44444");Thread thread=new Thread(new ThreadStart(new Class

iOS 页面与页面之间传参数的方法 代码块传值

代码块传值 是从后往前传值 1.声明代码块 (SecondXXX.h) 2.声明一个代码块类型的属性(SecondXXX.h) 3.调用代码块(SecondXXX.m) 4.实现代码块(SecondXXX.m) #import <UIKit/UIKit.h> #import "FirstViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property