线程 --中断

string notifyUrl = data["notifyurl"].ToString();
CancellationTokenSource tokenSource = new CancellationTokenSource();

//启用线程,得到回应"SUCCESS",中断线程,没有回应,连续发送5次信息
var t = System.Threading.Tasks.Task.Run(() =>
{
int num = 0;
//判断是否超过5次获取线程被取消
while (num < 5 && !tokenSource.IsCancellationRequested) { Thread.Sleep(2 * num); num++; string notidata = resjsondata; string rescontent = RequestHelper.HttpPost(notifyUrl, Encoding.UTF8, notidata); if (rescontent == "SUCCESS") { tokenSource.Cancel(); } } }, tokenSource.Token);
原文地址:https://www.cnblogs.com/AlexLeeLi/p/11950561.html