C# 多線程
- 使用背景:因為Webservice 調(diào)用公司郵件報警接口導(dǎo)致后續(xù)代碼執(zhí)行不了(提示網(wǎng)絡(luò)超時)需频,于是開一個線程
- C#中,線程入口是通過ThreadStart代理(delegate)來提供的,相當(dāng)于一個函數(shù)指針;指向線程要執(zhí)行的函數(shù)鳄抒,當(dāng)調(diào)用Thread.Start()方法后,線程就開始執(zhí)行ThreadStart 所代表或者說指向的函數(shù)搅窿。
- 使用多線程之前要先創(chuàng)建一個線程嘁酿。和新建一個類一樣,沒什么區(qū)別
//使用之前需要引用
using System;
using System.Threading;
namespace ESOP
{
public class Tands
{
public static string main(string userid)
{
Sent Sentd = new Sent();
Thread oThread = new Thread(new ParameterizedThreadStart(Sentd.SendEmail));
oThread.Start(userid);
return null;
}
}
public class Sent
{
public void SendEmail(object userId)()
{
//方法男应,業(yè)務(wù)邏輯
}
}
}