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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:12:02+00:00 2026-06-04T15:12:02+00:00

I am having a problem with a treeview where I try to add a

  • 0

I am having a problem with a treeview where I try to add a subnode based upon the Node.Text Index (I have also tried this based upon the int index – to no avail). This works wonderfully when ran synchronously. However I run the exact same thing Async (backgroundWorker) it throws an unhandled ArgumentOutOfRange Exception. Another odd part is that I’ve attempted to catch this Exception in two different areas. see code:

 using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
        {
            int x = 0;
            foreach (string subkey_name in key.GetSubKeyNames())
            {
                using (RegistryKey subkey = key.OpenSubKey(subkey_name))
                {
                    foreach (string a in (string[])subkey.GetValue("Users", ""))
                    {
                        User u = new User(a);
                        usrs.addUser(new User(a));
                        wgs.addUserToWorkgroup(subkey_name, a);
                        usrs.AddWorkGroupToUser(subkey_name, a);
                        int trycount = 0;
                    TryAgain:
                        try
                        {
                            //here is where the exception occurs
                            ExecuteSecure(() => treeView1.Nodes[subkey_name].Nodes.Add(a, a));
                        }
                        catch (ArgumentOutOfRangeException)//This does not catch it.
                        {
                            trycount++;
                            if (trycount < 100)
                            {
                                goto TryAgain; //b/c I cannot catch it this never happens...
                            }
                        }

                    }
                }

                x++;
                //System.Threading.Thread.Sleep(2);
                //As you can see I've tried to let the tread sleep to resolve this 
                //- it will get a little farther but still eventually bomb out.
            }
        }

Here is the ExecuteSecure code (https://stackoverflow.com/a/8021020/1387186)

 private void ExecuteSecure(Action a)
    {
        o = new object();
        try
        {
            if (InvokeRequired)
            {
                lock (o)
                {
                    BeginInvoke(a);
                }
            }
            else
                a();
        }
        catch (Exception) //again **sigh** this does not catch the error
        { }
  • 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-04T15:12:04+00:00Added an answer on June 4, 2026 at 3:12 pm

    You have several problems.

    • You are closing over the loop variable.
    • Your lock is pointless because you use a different instance each time.
    • Your lock is pointless because no other threads compete for it.
    • Your new solution of using Invoke cause the UI and worker threads to ping-pong back and forth and as such your worker thread now became useless.

    Anyone who monitors my answers on here knows how I feel about these marshaling techniques (like Invoke). It can be (and usually is) the worst method of updating the UI. In fact, it is so overused that it has gotten to the point where I think it can be considered a form of cargo cult programming.

    What you should do is have your worker thread publish the data required to update the TreeView into a queue and then have your UI thread pull it out via a System.Windows.Forms.Timer on an interval that works best for you. Here is what it would look like.

    public class YourForm : Form
    {
      private sealed class YourData
      {
        public string SubKeyName { get; set; }
        public string Value { get; set; }
      }
    
      private ConcurrentQueue<YourData> queue = new ConcurrentQueue<YourData>();
    
      private void StartTreeViewUpdate_Click(object sender, EventArgs args)
      {
        Task.Factory.StartNew(WorkerThread);
        TreeViewUpdateTimer.Enabled = true;
      }
    
      private void TreeViewUpdateTimer_Tick(object sender, EventArgs args)
      {
        // Update in batches of 100 (or whatever) so that the UI stays
        // responsive.
        treeView1.BeginUpdate();
        for (int i = 0; i < 100; i++)
        {
          YourData value = null;
          if (queue.TryDequeue(value) && value != null)
          {
            treeView1.Nodes[value.SubKeyName].Nodes.Add(value.Value, value.Value)
          }
          else
          {
            // We're done.
            TreeViewUpdateTimer.Enabled = false;
            break;
          }
        }
        treeView1.EndUpdate();
      }
    
      private void WorkerThread()
      {
        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
        {
          foreach (string subkey_name in key.GetSubKeyNames())
          {
            using (RegistryKey subkey = key.OpenSubKey(subkey_name))
            {
              foreach (string a in (string[])subkey.GetValue("Users", ""))
              {
                User u = new User(a);
                usrs.addUser(new User(a));
                wgs.addUserToWorkgroup(subkey_name, a);
                usrs.AddWorkGroupToUser(subkey_name, a);
                var data = new YourData();
                data.SubKeyName = subkey_name;
                data.Value = a;
                queue.Enqueue(data);
              }
              queue.Enqueue(null); // Indicate that queueing is done.
            }
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi The problem am having is that I have multiple TreeView control and each
Having a problem getting a TreeView control to display node images. The code below
I am having a problem moving a treenode in a treeview to a listbox.
I'm having problem when running my Windows Forms program. In the program, I have
I'm having problem selecting an element with an id like this <li =0f:Bactidol_Recorder.mp4>. I
I have been having problems with this for some time now, and have come
I am having problems with postbacks. I have a dropdownlist that i add items
Having problem with this bit of code qith jQuery. it should pick the values
I having problem to update the counter (integer value). this is the definitions of
m having problem in passing parameter to controller action, i have done the following

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.