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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:25:55+00:00 2026-06-03T21:25:55+00:00

I’ve seen this code lately: static List<Thread> list = new List<Thread>(); static void Main(string[]

  • 0

I’ve seen this code lately:

static List<Thread> list = new List<Thread>();

static void Main(string[] args)
{
    var lines = File.ReadAllLines(args[0]);

    foreach (var line in lines)
    {
        StartThread(line);
    }

    Console.WriteLine("JOIN");

    foreach (Thread thread in list)
    {
        thread.Join();
    }

    Console.WriteLine("END");
    Console.ReadKey();
}

static void Upsert(object o)
{
    var args = o.ToString().Split(',');

    try
    {
        using (var con = new SqlConnection(Settings.Default.ConnString))
        {
            var cmd = new SqlCommand
                          {
                              Connection = con,
                              CommandText = "INSERT INTO Accounts VALUES(@p1, @p2, @p3, @p4, @p5)"
                          };

            for (var index = 0; index < args.Length; index++)
            {
                cmd.Parameters.AddWithValue(@"@p" + (index + 1), args[index]);
            }

            try
            {
                con.Open();

                cmd.ExecuteNonQuery();

                Console.WriteLine("INSERTED");
            }
            catch (SqlException e)
            {
                switch (e.Number)
                {
                    case 2627:
                        cmd.CommandText =
                            "UPDATE Accounts SET Name=@p2, Email=@p3, Active=@p4, Birthday=@p5 WHERE ID = @p1";
                        cmd.ExecuteNonQuery();
                        Console.WriteLine("UPDATED");
                        break;
                    case 1205:
                        StartThread(o); // On exception isn't some Thread handling should happen?
                        break;
                }
            }
        }
    } 
}

private static void StartThread(object o)
{
    // Is it correct to add another thread to the list again? when exception happens? What about the thread that was running
    var t = new Thread(Upsert)
    {
        Priority = ThreadPriority.Highest,
        IsBackground = true
    };
    list.Add(t);
    t.Start(o);

    Console.WriteLine("NEW THREAD STARTED");
}

I’m not so strong on threading, and I was wondering about that code specificaly when the error 1205 could occur and run another thread again with the same method adding to te thread list one more time. Shouldn’t be a check about the previous exception cought thread if it is finished and abort it? Then remove it from the list and add the new one?

Your contribution is really helpful.

Thank you.

  • 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-03T21:25:56+00:00Added an answer on June 3, 2026 at 9:25 pm

    You are right there are several problems with this code.

    • Access to list is not synchronized in any way.
    • There is race between the main thread joining and a new thread being created after error 1205.

    I would scrap this code and create a stored procedure that does the upsert for you. Things like this are lot easier to deal with on the server side. Also, I am not particularly fond of the whole idea of multithreading for this situation anyway.

    If you are wanting maximum speed out of this then read the file in via C# code and parse it out so that it is broken up into individual fields. Then use SqlBulkCopy to throw all the records into a temporary landing zone table all at once. Finally, call a stored procedure to transfer the records in the temporary landing zone into the appropriate production table(s). All of this can be done without using any worker threads and it will probably be significantly faster as well.

    Update:

    The code can be fixed easily if you use CountdownEvent. Ditch the thread list entirely and instantiate a CountdownEvent to do the waiting instead of calling Join on all threads.

    static CountdownEvent complete = new CountdownEvent(1);
    
    static void Main(string[] args)
    {
        var lines = File.ReadAllLines(args[0]);
    
        foreach (var line in lines)
        {
            StartThread(line);
        }
    
        Console.WriteLine("JOIN");
    
        complete.Signal();
        complete.Wait();
    
        Console.WriteLine("END");
        Console.ReadKey();
    }
    

    Then change StartThread like this.

    private static void StartThread(object o)
    {
        complete.AddCount();
        var t = new Thread(
          () =>
          {
            try
            {
              Upsert(o);
            }
            finally
            {
              complete.Signal();
            }
          });
        t.Priority = ThreadPriority.Highest;
        t.IsBackground = true;
        t.Start();
        Console.WriteLine("NEW THREAD STARTED");
    }
    

    So what I am doing is initializing the CoundownEvent with 1 count because I want to treat the main thread as if it were a worker as well. This will fix any subtle race conditions that might arise if one of the workers finishes before the main thread has finished spinning up all of the other threads. Each time I start a new thread I call AddCount and when that thread finishes I call Signal. And of course, the main thread waits for everything by calling Wait.

    If I wanted to change the structure of the code a little more I would probably would have used tasks via Task and then when error 1205 comes I would have created a child task and attached it to the parent via TaskCreationOptions.AttachedToParent. But, that would have required some more significant changes and I wanted to keep the changes to the minimum.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.