gpt4 book ai didi

asp.net - signalR OnDisconnected() 取消任务

转载 作者:行者123 更新时间:2023-12-02 02:09:01 24 4
gpt4 key购买 nike

当页面加载时,我在我的中心开始了一个新任务。此任务将数据发送到浏览器以更新特定的 html 元素。当我离开页面浏览时,我想停止任务。

问题是在任务停止之前(由于它的 sleep 参数),一个新的 tokenSource = new CancellationTokenSource();在此任务的前一个实例有机会停止之前设置。

我想要做的是在离开页面浏览到不需要 signalR 的不同页面时停止任务。但是也许,如果它只是同一页面的刷新,则不要停止它。不知道该怎么做。总而言之,我想保证这个任务只有 1 个实例正在运行(并且只在需要它/或有一个监听器的页面上运行)非常感谢任何信息。谢谢

代码:

public class TaskActionStatus : Hub
{
#region Static Fields

/// <summary>
/// The token source.
/// </summary>
private static CancellationTokenSource tokenSource;

/// <summary>
/// The url string.
/// </summary>
private static string url = string.Empty;

#endregion

#region Public Methods and Operators

/// <summary>
/// The get tasks status.
/// </summary>
/// <param name="siteId">
/// The site id.
/// </param>
/// <param name="location"></param>
public void GetTasksStatus(int? siteId)
{
var taskRepository = UnityContainerSetup.Container.Resolve<ITaskRepository>();

tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;

// init task for checking task statuses
var tasksItem = new DownloadTaskItem();

// start task only if at least one listener
if (UserHandler.ConnectedIds.Count < 2 && !taskRepository.IsTasksStatusAsyncRunning())
{
taskRepository.GetTasksStatusAsync(siteId, tasksItem, ct);

// subscribe to event [ listener ]
tasksItem.Changed += this.UpdateTasksStatus;
}
else tokenSource.Cancel();
}

/// <summary>
/// The on connected.
/// </summary>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
public override Task OnConnected()
{
UserHandler.ConnectedIds.Add(this.Context.ConnectionId);
return base.OnConnected();
}

/// <summary>
/// The on disconnected.
/// </summary>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
public override Task OnDisconnected()
{
UserHandler.ConnectedIds.Remove(this.Context.ConnectionId);
if (UserHandler.ConnectedIds.Count == 0)
{
try
{
tokenSource.Cancel();
}
catch (Exception ex)
{
Log.Error(ex);
}
}

return base.OnDisconnected();
}

/// <summary>
/// The update tasks status.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
public void UpdateTasksStatus(object sender, TaskEventArgs e)
{
this.Clients.All.updateMessages(e.Tasks);
}

#endregion
}

/// <summary>
/// The user handler.
/// </summary>
public static class UserHandler
{
#region Static Fields

/// <summary>
/// The connected ids.
/// </summary>
public static HashSet<string> ConnectedIds = new HashSet<string>();

#endregion
}


public bool IsTasksStatusAsyncRunning()
{
if (tasksStatusAsync != null && tasksStatusAsync.Status.Equals(TaskStatus.Running))
{
return true;
}

return false;
}

最佳答案

移动这条线:

            tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;

...做成这样:

       if (UserHandler.ConnectedIds.Count < 2)
{
Trace.WriteLine("GetTasksStatus: Starting new task");

tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;

taskRepository.GetTasksStatusAsync(siteId, tasksItem, ct);

// subscribe to event [ listener ]
tasksItem.Changed += this.UpdateTasksStatus;
}

是为我做的。谢谢

关于asp.net - signalR OnDisconnected() 取消任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com