c#多线程②--多参数传递
程序员文章站
2022-05-02 17:42:49
...
1.定义参数类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class PersonInfo
{
public string Name { get; set; }
public string Code { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
}
2.线程传值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
public static int countShoe = 1;
public static bool isRun = true;
static void Main(string[] args)
{
PersonInfo andy = new PersonInfo() { Name = "Andy",Code="007",Age=26,Address="香港"};
for (int i = 0; i < 10; i++)
{
if (!isRun) {
i--;
continue;
}
Thread testThread = new Thread(()=>run(andy));
testThread.Start();
isRun = false;
}
}
static void run(PersonInfo person) {
if (countShoe == 0)
{
Console.WriteLine("抢完了");
}
else {
Console.WriteLine(person.Name+"抢购成功,客户编号:"+person.Code);
countShoe--;
}
isRun = true;
Console.ReadKey();
}
}
}
上一篇: C/C++ 创建多级文件夹(mkdir)
下一篇: 第12课:分布式锁