多线程调用传参
Thread thread=new Thread(Print);//穿入一个参数 thread.Start(参数);//参数传递在Start()方法中
分线程当中定义的方法,如果需要传递参数,参数的类型只能是object,而且只能是单参数
在线程调用的时候,把参数值传入到Thread.Start(参数值);
namespace 分线程调用传参
{
internal class Program
{
static void Main(string[] args)
{
//打印指定范围内的值 n
Thread thread = new Thread(Print);
//参数传递给Start方法
thread.Start(1000);
//打印一些名字
string[] names = { "张三", "李四", "王五" };
Thread thread2 = new Thread(PrintName);
thread2.Start(names);
}
public static void PrintName(object obj)
{
string[] arr = obj as string[];
foreach (string name in arr)
{
Console.WriteLine(name);
}
}
//定义方法打印前N项的值
public static void Print(object n)
{
int num = (int)n;
for (int i = 1; i <= num; i++)
{
Console.WriteLine(i);
}
}
}
}
练习
namespace 幸运抽奖
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thread;
//绑定点击事件
private void button1_Click(object sender, EventArgs e)
{
string[] names = { "张三", "李四", "王五", "任六", "甲", "乙", "丙",
"丁", "一", "二", "三", "四", "无", "六", "七" };
Button btn = sender as Button;
if (btn.Text == "开始抽奖")
{
&nbs

最低0.47元/天 解锁文章
5603

被折叠的 条评论
为什么被折叠?



