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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:28:04+00:00 2026-05-23T16:28:04+00:00

Here is my Timer Elapsed Event, I am receiving the System.OutOfMemoryException on the line

  • 0

Here is my Timer Elapsed Event, I am receiving the System.OutOfMemoryException on the line Thread thread = new Thread(threadStart);
I am receiving the error fairly fast (1~5 minutes, randomly), and it does not cause unexpected results in my program. I am just wondering what is causing this error, and I am afraid it may cause unexpected results if it is left unchecked. I have searched on the internet and am comming no where near the number of max threads.
readList contains about 46 enteries.

Any help would be appreciated.

private void glob_loopTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    try
    {
        ParameterizedThreadStart threadStart = new ParameterizedThreadStart(readHoldingRegisters);
        foreach (readwriteDataGridRow.Read row in readList)
        {
            Thread thread = new Thread(threadStart);
            thread.IsBackground = true;
            thread.Start(System.Convert.ToInt32(row.Address));
        }
    }
    catch (Exception ex)
    {
        UpdateConsole(new object[] { ex.Message.ToString() + " " + ex.StackTrace.ToString(), Color.Red });
        Thread.CurrentThread.Abort(); // maybe?
    }
}

EDIT:
Here is a bit more information.
My program is reading registers from a Serial Device using the Modbus RTU protocol.
A single register takes less than a tenth of a second to retrieve from readHoldingRegisters

I am open to suggestions on what else to use rather than threads.
note: I need to call readHoldingRegisters 40 – 100 times in a single ‘pass’. The passes start when the user hits connect and end when he hits disconnect. Timers are not needed, they just offered a simple way for me to maintain the loop with a start and stop button.

EDIT: Solved

    private void glob_loopTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    try
    {
        foreach (readwriteDataGridRow.Read row in readList)
        {
              readHoldingRegisters(row.Address);
        }
    }
    catch (Exception ex)
    {
        UpdateConsole(new object[] { ex.Message.ToString() + " " + ex.StackTrace.ToString(), Color.Red });
    }
}

The additional Threads were the problem and were not needed.

  • 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-23T16:28:04+00:00Added an answer on May 23, 2026 at 4:28 pm

    Ughh, do not, ever (well almost ever) abort threads. There are many preferable ways to make a System.Thread stop. Look around SO, you will find plenty of examples on why doing this is a bad idea and alternative approaches.

    On with your question: The problem doesn’t seem to be the number of rows in readList. It is more likely that your glob_looperTimer_Elapsed event handler is being executed many times and you are basically starting more and more threads.

    What is the interval of your glob_loopTimer?

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

Sidebar

Related Questions

There is a problem with standard System.Timers.Timer behaviour. The timer raise Elapsed event with
Possible Duplicate: System.Timer elapsed event seems to fire late for short intervals in .Net
I'm writing windows service which will process something every couple minutes. Here is some
I want to display, on screen, the elapsed time since some event. I have
static Timer _timer; static void Main(string[] args) { _timer = new Timer(1000); _timer.Enabled =
I'm using System.Timers.Timer in my WPF application. I want to understand how Timer does
I have a loop and within that loop I'm running a Dispatcher Timer. Here's
I have an issue with the System.Timers.Timer object. I use the timer object to
Hi I am working with Windows.Forms.Timer with Web Application . I create Timer.Tick event
This is my first time here so I hope I post this question at

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.