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

c# 多线程传递参数之解决方案

2009-12-30 20:24 246 查看
刚才在想一个问题 就是多线程调用方法的时候不能传递参数

这个问题挺麻烦的

如果我要传递参数去调用方法该怎么办呢?

在网上找了一下

下面是我的一个解决方案

 

建立一个类

这个类中保存你要调用的方法 返回值赋给属性

然后调用这个属性

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

for (int i = 0; i < 100; i++)
{
Person p = new Person(2, 3);
Thread thread = new Thread(new ThreadStart(p.Sum));
thread.Name = "线程"+i;
thread.Start();
Thread.Sleep(50);
Console.Write(thread.Name+":"+p.C);

}

Console.ReadLine();
}
}

class Person
{
int a, b;
private int c;

public int C
{
get { return c; }
set { c = value; }
}
public Person(int a,int b)
{
this.a = a;
this.b = b;
}

public void Sum()
{
c = a + b;
}
}
}


 

 

发个牢骚:今天无意中在CSDN上的一个帖子上看到一个F#的讲述

我靠

我C#还没学好  F#就快商业化 了  微软还让不让我们程序员活了。。。

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