DoEvents

by pietman 16. December 2011 14:46

System.Windows.Forms.Application.DoEvents();

Tags:

Threading

threading - parameterized

by pietman 16. December 2011 12:22
lambda: Thread thread = new Thread(() => download(filename));

c# 2.0 Thread thread = New Thread(delegate() { download(filename); });

long way:
var thread = new Thread(new ParameterizedThreadStart(download));

others:

 ThreadPool.QueueUserWorkItem( () => value = Process(value)); // use captured variable

 
// -- OR --

 
ThreadPool.QueueUserWorkItem( () =>
     
{
           
double val = value;      // use explicit parameter
           val
= Process(val);      // v is not changed
     
});

 
// -- OR --

 
ThreadPool.QueueUserWorkItem( (v) =>
     
{
           
double val = (double)v;  // explicit var for casting
           val
= Process(val);      // v is not changed
     
}, value);



BOOKS!

Tags:

Threading

threading invokeRequired invoke

by pietman 1. October 2010 15:29

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();
        }
    }

 

Tags:

Threading

About ...

pietman celliersPietman Celliers
Bitlink  Ltd
bitlinkit.com