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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:39:06+00:00 2026-06-17T21:39:06+00:00

In my c# windows service i’m starting several threads. Each thread should dispose it’s

  • 0

In my c# windows service i’m starting several threads. Each thread should dispose it’s objects after a specific period of time with no activity.

I tried the System.Threading.Timer and System.Timers.Timer classes but they all raise the events on a different thread.

This is what’s happining:

  • create several threads during service.start
  • each thread monitors a directory and activates a resource extensive function for every new file
  • After a 1 hour of no activity inside the thread (no files are handled during that period) i want to cleanup the resources inside that thread (structuremap, threadlocal classes)
  • 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-17T21:39:07+00:00Added an answer on June 17, 2026 at 9:39 pm

    Assuming that by “a period of no activity” you mean a thread that has been in the WaitSleepJoin for too long, you can interrupt such a thread by calling its Interrupt Method:

    var t = new Thread(WorkerThred);
    t.Start();
    
    // when the thread's been inactive for too long, kill it!
    t.Interrupt();
    

    This will cause a ThreadInterruptedException to be thrown in the target thread. Hence, you can catch the exception in order to dispose the thread’s resources:

    private void WorkerThred()
    {
        try
        {
            DoWork();
        }
        catch (ThreadInterruptedException)
        {
            DisposeResources();
        }
    }
    

    Now you need some way of tracking the amount of time each thread stays inactive. For instance, create a method that all threads must call before going to sleep or waiting on a mutex:

    public static void UpdateLastActiveTime()
    {
        var t = Thread.CurrentThread;
        if (!lastActiveTime.ContainsKey(t))
            lastActiveTime.Add(t, DateTime.Now);
        else
            lastActiveTime[t] = DateTime.Now;
    }
    
    private static Dictionary<Thread, DateTime> lastActiveTime;
    

    Finally, to check for how long a specific thread has been inactive, call the following method:

    public static TimeSpan GetInactiveTime(Thread t)
    {
        // if thread isn't sleeping anymore update its time
        if (t.ThreadState != ThreadState.WaitSleepJoin)
            lastActiveTime[t] = DateTime.Now;
        return DateTime.Now - lastActiveTime[t];
    }
    

    I believe that using this pattern you will be able to create a thread manager class that meets your requirements.

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

Sidebar

Related Questions

I have a windows service which runs continuously and creates some threads to do
I have a .NET Windows service that runs multiple threads. A developer added some
My windows service is using a Thread (not a timer) which is always looping
My Windows service is able to launch threads (suing the ThreadStart delegate) in Win
I'm writing a windows service that needs to sleep for long periods of time
I have a Windows Service in C#. I want a certain thread to perform
Whenever a specific Windows service fails I want to run a program I've created
I creating an windows service which will from time to time check if a
I'm writing a windows service in .net 2.0. What should I do within the
I have a windows Service that start some tasks based on a configuration. Each

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.