手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#创建线程带参数的方法
C#创建线程带参数的方法
摘要:1、无参数线程的创建Threadthread=newThread(newThreadStart(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#创建线程带参数的方法 ,希望对大家有所帮助,如果大家有任何疑问请给我们留言,小编会及时回复大家的。在此也非常感谢大家对查字典教程网的支持!

【C#创建线程带参数的方法】相关文章:

C# 禁用鼠标中间键的方法

C#异步调用的好处和方法分享

利用多线程句柄设置鼠标忙碌状态的实现方法

C# 去除首尾字符或字符串的方法

C#计算代码执行时间的方法

配置C#的系统环境变量的方法

c#中虚函数的相关使用方法

C# IP地址与整数之间转换的具体方法

C# DropDownList中点击打开新窗口的方法

使用checked语句防止数据溢出的解决方法

精品推荐
分类导航