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

  • Home
  • SEARCH
  • 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 1044587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:51:07+00:00 2026-05-16T15:51:07+00:00

how to stop executing code in thread if takes to long. I have few

  • 0

how to stop executing code in thread if takes to long. I have few threads which working all the time but after while code in thread is executing too long and application stop responding.

is it possible that if code is not executed in 30s thread will stop executing it and go to next code… so application will still be alive and will not stop responding. i am using C# .net 3.5

  • 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-05-16T15:51:07+00:00Added an answer on May 16, 2026 at 3:51 pm

    My answer here is similar to the one I posted here.

    You can do this by waiting on your worker thread from a monitoring thread for a specified amount of time and then forcefully killing the worker thread if it hasn’t already completed. See the example code below.

    In general, however, killing a thread forcefully with Thread.Abort is not a good idea since the target thread is not necessarily in a known state and could have open handles to resources that might not be freed. Using Thread.Abort is a code smell.

    The cleaner way is to change the worker thread to manage its own lifetime. The worker thread could check how long it has executed at well-known checkpoints and then stop if it has exceeded some limit. This approach has the drawback of requiring potentially many checkpoints scattered throughout the actual work the thread is doing. Also, the worker thread could easily exceed a limit by doing too much computation between checkpoints.

    class Program
    {
        static void Main(string[] args)
        {
            if (RunWithTimeout(LongRunningOperation, TimeSpan.FromMilliseconds(3000)))
            {
                Console.WriteLine("Worker thread finished.");
            }
            else
            {
                Console.WriteLine("Worker thread was aborted.");
            }
        }
    
        static bool RunWithTimeout(ThreadStart threadStart, TimeSpan timeout)
        {
            Thread workerThread = new Thread(threadStart);
    
            workerThread.Start();
    
            bool finished = workerThread.Join(timeout);
            if (!finished)
                workerThread.Abort();
    
            return finished;
        }
    
        static void LongRunningOperation()
        {
            Thread.Sleep(5000);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a code that will start two threads upon executing public class
When writing server-side code you need to explicitly stop execution after sending a Location:
I'm developing tool, which takes Java code and generate code for getting estimations for
I have been working on this issue for a few days now and can't
I have successfully gotten my low-level mouse hook code to work, but there are
I have a thread, A , which performs periodic actions inside a while(running) loop.
I have some code that can spawn background worker threads from anywhere within it.
I have this simple code: public void Run() { var invokerThread = new Thread(new
If I have a class delete itself, should its internal methods stop executing? I
I have a process that must create and close threads on demand. Each thread

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.