线程调用带参数的方法:有两种方式:
1、使用委托:
static void Main(string[] args)
{
/*线程学习*/
Thread thread1 = new Thread(new ThreadStart(delegate{ Program.method1("haha"); }));
Thread thread2 = new Thread(new ThreadStart(method2));
thread1.IsBackground = true;
thread1.Start();
thread2.Start();
Console.ReadKey();
}
/*线程学习所用方法*/
public static void method1(string s)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")+"method1"+s);
}
public static void method2()
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")+"mm11111");
}
/*线程学习所用方法 */
2、使用obj类型参数的方法,见method1,参数类型火object
static void Main(string[] args)
{
/*线程学习*/
Thread thread1 = new Thread(method1); //这里可以用 类名.方法
Thread thread2 = new Thread(method2);
thread1.Start("haha");
thread2.Start();
Console.ReadKey();
}
/*线程学习所用方法*/
public static void method1(object s)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")+"method1"+s);
}
public static void method2()
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")+"mm11111");
}
/*线程学习所用方法 */
快捷键:在波浪线的名字上,shift+alt+f10,可以引入命名空间: