当前位置: 移动技术网 > IT编程>开发语言>c# > C#创建线程带参数的方法

C#创建线程带参数的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
1、无参数线程的创建 thread thread = new thread(new threadstart(getpic)); thread.start();

1、无参数线程的创建

thread thread = new thread(new threadstart(getpic));
thread.start();
private void showmessage()
{
console.writeline("hello world");
}

2、带一个参数的线程

使用parameterizedthreadstart,调用 system.threading.thread.start(system.object) 重载方法时将包含数据的对象传递给线程。

注意传递的参数只能是object类型,不过可以进行强制类型转换。

thread thread = new thread(new parameterizedthreadstart(showmessage));
string o = "hello";
thread.start((object)o);
private static void showmessage(object message)
{
string temp = (string)message;
console.writeline(message);
}

3、带两个及以上参数的线程

这时候可以将线程执行的方法和参数都封装到一个类里边,通过实例化该类,方法就可以调用属性来尽享传递参数。

例如如下程序,想传入两个string变量,然后打印输出。

public class threadtest
{
private string str1;
private string str2;
public threadtest(string a, string b)
{
str1 = a;
str2 = b;
}
public void threadproc()
{
console.writeline(str1 + str2);
}
}
public class example {
public static void main() 
{
threadtest tt = new threadtest("hello ", "world");
thread thread = new thread(new threadstart(tt.threadproc));
thread.start();
}
}

以上所述是小编给大家介绍的c#创建线程带参数的方法 ,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网