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 9117567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:55:33+00:00 2026-06-17T04:55:33+00:00

I am building a multi-threaded ASP.NET application. I am new to writing multi-threaded apps.

  • 0

I am building a multi-threaded ASP.NET application. I am new to writing multi-threaded apps. I have a main class that spawns a number of threads that run while the application is being used. The application has one main HTML page. When the HTML page is first opened it calls the MyController.Start() method to create the threads and get them running. Then the HTML page has a Javascript routine that continually polls an ASPX page in my application (every 2 seconds) to get data and display it. There is a main class I called “MyController” that is the heart of the application. It has a local boolean variable called Cancelled that is set to false. The MyController spawns all of the child threads. Each child thread has a loop that continues to execute as long as Cancelled = false. One of the threads that is running is used as a monitor to make sure that the HTML page is still up and polling. If this thread notices that 30 seconds have expired since the last time the HTML page polled then it changes the Cancelled variable to true. Then one by one all of the threads finish running and close gracefully.

I need to be able to handle the situation where the user closes the browser and then reopens it before the 30 seconds has expired. I don’t feel like I completely understand what the relationship is between the threads that get created and started and the instance of MyController. Once threads are created and started do they just run independently of anything else? I have a main static class called App that is used to provide easy access to the controller from anywhere in the application. The way I have it coded right now I think that I am creating new threads every time that the HTML page is loaded. So if the user were to close the HTML page and then reopen it I think then I would have two instances of every thread running. Is that correct? If so, how can I check to see if those threads are already running so that I don’t create new instances of them?

Hope this makes sense! Thanks in advance for any help. Here is my code.

Start.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
  if(App._myController == null)
  {
    App._myController = new MyController();
  }
  App._myController.Start();
}

Library code

public class App
{
  public static MyController _myController {get;set;}
  static App()
  {
    _myController = new MyController();
  }
}

public class MyController
{
  public bool Cancelled {get;set;}
  private object SyncLock = new object();
  Thread _thread1;
  Thread _thread2;
  Thread _thread3;

  public void Start()
  {
    this.Cancelled = false;
    _thread1 = new Thread(Worker1);
    _thread1.Start();

    _thread2 = new Thread(Worker2);
    _thread2.Start();

    _threadMonitor = new Thread(Monitor);
    _threadMonitor.Start();
  }

  public void Stop()
  {
    lock(this.SyncLock)
    {
      this.Cancelled = true;
    }
  }

  private void Worker1()
  {
    while(!Cancelled)
    {
      ..do some stuff..
    }
  }

  private void Worker2()
  {
    while(!Cancelled)
    {
      ..do some stuff..
    }
  }

  private void Monitor()
  {
    while(!Cancelled)
    {
      ..check database to see when last poll was
      ..if last poll is greater than 30 seconds ago
      {
        this.Stop();
      }
    }
  }

}
  • 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-17T04:55:35+00:00Added an answer on June 17, 2026 at 4:55 am

    I’m not in love with your design, but that’s a separate question. I’ll address your current problem with your current design…

    From your Start() function, if the threads are not null, set Cancelled to true and Join() the threads, then continue with your existing functionality.

      public void Start()
      {
        this.Cancelled = true;
    
        if (_thread1 != null) _thread1.Join();
        // repeat for other threads
    
        this.Cancelled = false;
        _thread1 = new Thread(Worker1);
        _thread1.Start();
    
        _thread2 = new Thread(Worker2);
        _thread2.Start();
    
        _threadMonitor = new Thread(Monitor);
        _threadMonitor.Start();
      }
    

    As for a better approach, maybe a service to do the background work that would interface with your page using WCF (perhaps using REST).

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

Sidebar

Related Questions

I am building a multi-layered application that has an ASP.NET MVC web application. It
I'm building a multi-tenant ASP .NET application. Given that each tenant can configure their
I'm building a multi-tenant app with ASP.NET MVC and have a problem with validating
I'm building a multi-threaded client side javascript application, and I would like to have
I have a specific question, that could use a general answer... When building Multi-Tier
I am building a multi threading application. I have setup a threadPool. [ A
I am building up a multi-threaded application where I spawn three threads when application
I have a multi-dimensional array that I'd like to use for building an xml
I am building a multi language site I have a descriptions table that hold
I'm building a multithreaded app in .net. I have a thread that listens to

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.