首页 / C# / c# – 取消ASP.NET启动的任务
c# – 取消ASP.NET启动的任务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 取消ASP.NET启动的任务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2742字,纯文字阅读大概需要4分钟。
内容图文
![c# – 取消ASP.NET启动的任务](/upload/InfoBanner/zyjiaocheng/781/472d0cbf46cc49c78e1010ce5d48d82f.jpg)
背景
以下服务器端代码用于启动长时间运行的任务,该任务将通过SignalR将更新发布到Web前端.我在前端放了一个按钮,我想根据用户的要求停止任务.
问题
当前端触发器的Stop方法时,tokenSource为null.我怀疑,这是因为它没有到达产生任务的ChartHub的同一个实例.
码
using System;
...
using System.Security.Principal;
namespace dvvWeb.Hubs
{
public class ChartHub : Hub
{
CancellationTokenSource tokenSource;
CancellationToken ct;
public void Start(string serverName, string dbName, string numberOfPoints, string pollingFrequency)
{
ConfigModel config = new ConfigModel();
tokenSource = new CancellationTokenSource();
ct = tokenSource.Token;
config.Servername = HttpUtility.UrlDecode(serverName);
config.DbName = HttpUtility.UrlDecode(dbName);
config.Preferences.NumberOfPoints = int.Parse(numberOfPoints);
config.Preferences.PollingFrequency = int.Parse(pollingFrequency);
dvvGraphingModel graphingModel = new dvvGraphingModel();
dvvGraphingHelper graphingHelper = new dvvGraphingHelper(graphingModel, config.Servername, config.DbName);
graphingModel = graphingHelper.Tick(config.Preferences);
var identity = WindowsIdentity.GetCurrent();
Task.Run(() => workItemAsync(ct, graphingModel, graphingHelper, config, identity));
}
public void Stop()
{
tokenSource.Cancel();
}
private async Task<CancellationToken> workItemAsync(CancellationToken ct, dvvGraphingModel graphingModel, dvvGraphingHelper graphingHelper, ConfigModel configModel, WindowsIdentity identity)
{
await addDataAsync(ct, graphingModel, graphingHelper, configModel, identity);
return ct;
}
private async Task<CancellationToken> addDataAsync(CancellationToken ct, dvvGraphingModel graphingModel, dvvGraphingHelper graphingHelper, ConfigModel configModel, WindowsIdentity identity)
{
try
{
while(!ct.IsCancellationRequested)
{
identity.Impersonate();
Clients.Caller.addPointToChart(JsonConvert.SerializeObject(graphingModel));
System.Threading.Thread.Sleep(configModel.Preferences.PollingFrequency * 1000);
graphingModel = graphingHelper.Tick(configModel.Preferences);
}
}
catch (TaskCanceledException tce)
{
Trace.TraceError("Caught TaskCanceledException - signaled cancellation " + tce.Message);
}
return ct;
}
}
}
解决方法:
我会创建一个ConcurrentDictionary< string,CancellationTokenSource>其中’string’将是用户名/ id或者可能
ConcurrentDictionary< IUserIdentity,CancellationTokenSource>.
那么,在这种情况下,用户一次只能启动一个进程.
该词典将存在于Hub之外的Singleton类中.您的集线器只是您单身人士中调用方法的代理.
YourSingleton.Instance.Start(userId, serverName, dbName, numberOfPoints, pollingFrequency);
和
YourSingleton.Instance.Stop(userId);
然后,你可以这样做:
public void Stop(string userId)
{
CancellationTokenSource tokenSource;
if(dictionary.TryGetValue(userId, out tokenSource))
{
tokenSource.Cancel();
dictionary.TryRemove(userId out tokenSource);
}
}
内容总结
以上是互联网集市为您收集整理的c# – 取消ASP.NET启动的任务全部内容,希望文章能够帮你解决c# – 取消ASP.NET启动的任务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。