C# 多线程
- 使用背景:因为Webservice 调用公司邮件报警接口导致后续代码执行不了(提示网络超时),于是开一个线程
- C#中,线程入口是通过ThreadStart代理(delegate)来提供的,相当于一个函数指针;指向线程要执行的函数,当调用Thread.Start()方法后,线程就开始执行ThreadStart 所代表或者说指向的函数。
- 使用多线程之前要先创建一个线程。和新建一个类一样,没什么区别
//使用之前需要引用
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)()
{
//方法,业务逻辑
}
}
}