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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:23:49+00:00 2026-06-15T06:23:49+00:00

I am a newbie to C# and Threading and this is a very simple

  • 0

I am a newbie to C# and Threading and this is a very simple question, but has me really stuck. I did search on this site but could not find an answer similar to my scenario:

I have a method say Parent() and in that I create a typed list, every nth time I pass it off to a Task. I have the problem of when to clear the list and release the memory since it keeps growing. I tried clearing list at the end of the task, if I clear list with the Parent method, the list is empty in the thread.

Could someone help me? I know this is a very simple problem, but would appreciate the help.

    public void Parent()
    {
     List<MyType> list = new List<MyType>();
     for (int i = 0; i< N; i++)
     {
        list.Add(new MyType {Var = "blah"});

      if ( i% 10 == 0) //every tentth time we send a task out tou a thread
      {
       Task.Factory.StartNew(() => WriteToDB(new List<MyType>(list))); 
       //here I am              sending a new instance of the list

        //Task.Factory.StartNew(() => WriteToDB((list))); 
        //here I am sending same instance

        list.Clear();

         //if I clear here the list sent to the WriteToDB is empty
        //if I do not, the memory keeps growing up and crashes the app 
      }

      private void WriteToDB(List<MyType> list)
      {
       //do some calculations with the list 
       //insert into db 
       list.Clear(); 
      }
     }
   }
  • 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-15T06:23:50+00:00Added an answer on June 15, 2026 at 6:23 am

    You’ve got a closure bug.

    The lambda () => WriteToDB(new List<MyType>(list)) isn’t executed until the new Task starts.
    This will sometimes be after your call to list.Clear().

    The fix is to capture the copy of the list outside the lambda:

    var chunk = new List<MyType>(list);
    Task.Factory.StartNew(() => WriteToDB(chunk));
    
    list.Clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
Newbie here with a frustrating, but probably simple question. I am trying to write
Newbie question here but for some reason I cant figure this out. I have
this is a totally newbie question since my field is Java and not C#
Newbie question here. I'm in the beginning stages of laying out a site in
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
I'm a multiprocessing newbie, I know something about threading but I need to increase
Newbie question: I am making a simple html game. It consists of these DIV
is this correct way of doing.i am a newbie python class main(threading.Thread): queueLock =
I'm a newbie to threading, and I don't really know how to code a

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.