Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:03:26+00:00 2026-06-15T19:03:26+00:00

When a page loads I start a new task in my hub. This task

  • 0

When a page loads I start a new task in my hub. This task sends data to a browser updating a certain html elements. When I browse away from the page I want to stop the task.

The problem is that before the task is stopped (due to it’s sleep argument), a new tokenSource = new CancellationTokenSource();
is set before the previous instance of this task had a chance to stop.

What i’m trying to do is to have the task stop when browsing away from the page to a different page that is not requiring signalR. BUT maybe, not stop it if it’s only a refresh of the same page. Not sure how to do it. To sum it up, I want to guarantee that only 1 instance of this task is running (AND only on the page that requires it/or have a listener)
any info greatly appreciated.
thanks

CODE:

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:03:27+00:00Added an answer on June 15, 2026 at 7:03 pm

    Moving this line:

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

    …
    making it this:

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

    did it for me. thanks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html page that open a popup window when the page loads.
Let's say we have a page written in PHP. This page loads by it
I've created a start page that loads when the application is run but it
I am making a small HTML page editor. The editor loads a file into
I'm trying to use the BackgroundWorker class to start a new thread which loads
My page loads a TextView which is scrollable. Unfortunately the position appears to stick
My Jquery ajax page loads and shows up in the DIV that lays itself
When my Silverlight page loads, I want to set focus on a control. Simple
After my page loads my text disappears and I am having trouble determining why.
everytime my page loads, im supposed to create a datatable (also a jquery plugin)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.