您的位置:首页 > 编程语言 > C#

C#线程调用有参和无参函数的方法

2014-01-03 15:25 288 查看
引用命名空间:using System.Threading; 

一、调用无参函数 

Thread th = new Thread(new ThreadStart(Function)); 

th.Start(); 

private static void Function() 



   //被线程调用的方法



二、调用有一个参数的函数 

 Thread th = new Thread(new ParameterizedThreadStart(Function)); 

  th.Start("需要传递的参数"); 

 private static void Function(object obj) 

  { 

        //被线程调用的方法   

  } 



三、调用有多个参数的函数 

 MyClass m = new MyClass(); 

  m.name = "佚玥"; 

  m.age =24; 

  Thread th = new Thread(new ThreadStart(m.Function)); 

  th.Start(); 

    //自己定义的参数传递类

    class MyClass

    { 

        public string name;
public int age;
//自己需要的参数 都 设置 成 字段 或属性 

        public void Function() 

        { 

            //被线程调用的方法   

        } 

    } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息