线程 --中断

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);

 

posted @ 2019-11-28 15:55  AlexLeeLi  阅读(99)  评论(0编辑  收藏  举报