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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:32:30+00:00 2026-06-04T18:32:30+00:00

I’m having some troubles with ending a group of dynamically created threads. The reason

  • 0

I’m having some troubles with ending a group of dynamically created threads. The reason i need to end all these at any given point is so that i can refresh certain parts of my form and create new ones. Heres a simple scenario to show what happens in my threads.

A number of threads are created dynamically based upon certain variables inplace:

 for (int i = 0; i <= mDevices.Count; i++)
        {
            ThreadStart workerThread = delegate { pollDevices(mDevices[i - 2], this); };
            new Thread(workerThread).Start();
        }

  public void pollDevices(IDeviceInterface device, DeviceManager mainUI)
    {
       System.Timers.Timer timer = null;
        if (timer == null)
        {

            timer = new System.Timers.Timer(1000);
            timer.Elapsed += delegate(object sender, ElapsedEventArgs e) { timerElapsed(sender, e, device, mainUI); };

        }
        timer.Enabled = true;

 public void timerElapsed(object sender, ElapsedEventArgs e, IDeviceInterface device, DeviceManager mainUI)
    {


         device.connect(device, this);
        //wait till thread finishes and destroy
         Thread.CurrentThread.Abort();
    }

these threads then work off of a timer, and proc an event, which handles UI updates and such. however, when i try and refresh the UI (for example if any more entries in the database need taking into account) i get errors from deleting buttons on the form (these are assigned to a thread) if a thread is still running, so before i call for the refresh i need to stop all current threads in this manner.

so my question is, how can i abort this group of threads before i refresh my UI?

  • 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-04T18:32:31+00:00Added an answer on June 4, 2026 at 6:32 pm

    You have several problems.

    • You are closing over a loop variable.
    • You are creating a thread for the sole purpose of starting a timer…why not just start the timer in the main thread?
    • Your timer instance is not rooted so it is possible that it will be eligible for garbage collection before you are done with it.
    • The System.Timers.Timer event handler will be executed on a ThreadPool thread (at least in this case) so trying to abort one of these threads is not going to end well.

    There are enough problems here that you are going to do some significant restructuring before we can answer your main question. One additional tip though…do not try to access or manipulate any UI elements from a thread other than the main UI thread.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT

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.