-
using System;
-
using System.Threading;
-
namespace MutiThreadSample
-
{
-
/// <summary>
-
/// 创建线程的方式
-
/// </summary>
-
class CreateThread
-
{
-
/// <summary>
-
/// 不带参数的委托
-
/// </summary>
-
public void CreateThreadWithThreadStart()
-
{
-
Thread thread = new Thread(new ThreadStart(ThreadCallBack));
-
thread.Start();
-
}
-
/// <summary>
-
/// 带参数的委托
-
/// </summary>
-
public void CreateThreadWithParamThreadStart()
-
{
-
Thread thread = new Thread(new ParameterizedThreadStart(ThreadCallBackWithParam));
-
Object param = null;
-
thread.Start(param);
-
}
-
/// <summary>
-
/// 匿名函数
-
/// </summary>
-
public void CreateThreadWithAnonymousFunction()
-
{
-
Thread thread = new Thread(delegate()
-
{
-
Console.WriteLine("进入子线程1");
-
for (int i = 1; i < 4; ++i)
-
{
-
Thread.Sleep(50);
-
Console.WriteLine("\t+++++++子线程1+++++++++");
-
}
-
Console.WriteLine("退出子线程1");
-
});
-
thread.Start();
-
}
-
/// <summary>
-
/// 直接赋值委托
-
/// </summary>
-
public void CreateThreadWithCallBack()
-
{
-
Thread _hThread = new Thread(ThreadCallBack);
-
_hThread.Start();
-
}
-
/// <summary>
-
/// 无参数的方法调用
-
/// </summary>
-
public void ThreadCallBack()
-
{
-
// Do Something
-
}
-
/// <summary>
-
/// 带参数的方法
-
/// </summary>
-
/// <param name="obj"></param>
-
public void ThreadCallBackWithParam(object obj)
-
{
-
// Do Something
-
}
-
}
-
}
C# 几种创建线程的方法
最新推荐文章于 2024-07-18 11:38:09 发布