.

threading invokeRequired invoke

The following is a demonstration of how the invokeRequired can be used in Threading:

public partial class Form1 : Form
    {
        private System.Windows.Forms.TreeView treeView1;
 
        public Form1()
        {
            this.treeView1 = new System.Windows.Forms.TreeView();
        }
        Microsoft.Win32.RegistryKey[] allkeys = new Microsoft.Win32.RegistryKey[]
        {
            Microsoft.Win32.Registry.ClassesRoot,
            Microsoft.Win32.Registry.CurrentConfig,
            Microsoft.Win32.Registry.CurrentUser,
            Microsoft.Win32.Registry.DynData,
            Microsoft.Win32.Registry.LocalMachine,
            Microsoft.Win32.Registry.PerformanceData,
            Microsoft.Win32.Registry.Users
        };
 
 
        private void RefreshRegistryListView()
        {
            if (treeView1.InvokeRequired)
            {
                treeView1.Invoke(new MethodInvoker(RefreshRegistryListView));
                return;
            }
            else
                UpdateTheTreeview(this, null);
        }
 
        private void UpdateTheTreeview(object Sender, EventArgs e)
        {
            TreeNode TN = new TreeNode();
            Dictionary<string, TreeNode> RegistryNodes = new Dictionary<string,TreeNode>();
            foreach (Microsoft.Win32.RegistryKey key in allkeys)
            {
                TreeNode T = TN.Nodes.Add(key.Name);
                RegistryNodes.Add(key.Name, T);
            }
            foreach (Microsoft.Win32.RegistryKey key in allkeys)
            {
                Application.DoEvents();
                try
                {
                    foreach (string s in key.GetSubKeyNames())
                        RegistryNodes[key.Name].Nodes.Add(s);
                }
                catch { }
            }
            treeView1.Nodes.Clear();
            foreach (TreeNode key in RegistryNodes.Values)
                treeView1.Nodes.Add(key);
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread oThread = new Thread(new ThreadStart(RefreshRegistryListView));
            oThread.Start();
        }
    }

 

What's your thoughts on this?

*

Protected by WP Anti Spam