Loading Treeview in the Background
-
I have a large treeview I need to build and it takes about 1 minute to load. what I'd like to do is load the treeview in background. I tried doing it through, but it causes the main form to freeze until it's complete. Does anyone know what the best process is to load things in the background not affecting the main application screen. Thanks.
-
I have a large treeview I need to build and it takes about 1 minute to load. what I'd like to do is load the treeview in background. I tried doing it through, but it causes the main form to freeze until it's complete. Does anyone know what the best process is to load things in the background not affecting the main application screen. Thanks.
-
Or even easier (if you're using .NET 2.0) is to drop a BackgroundWorker process onto the form. It's DoWork event will fire a method on the form where you can do your processing. Be warned however, that Windows Forms aren't thread safe, so any manipulation of the form controls requires jumping back onto the main thread using Control.Invoke or Control.BeginInvoke to call a delegate. Eg.
public partial class MyForm { // Your delegate definition. It basically defines the prototype of the method. delegate void AddItemDelegate(string text); // Method for adding a single item to your list private void AddItem(string text) { if (InvokeRequired) { // We're not in the UI thread, so we need to call BeginInvoke BeginInvoke(new AddItemDelegate(AddItem), new object[] { text }); return; } myListBox.Items.Add(text); } // Your DoWork event private void myListBox_DoWork(object sender, DoWorkEventArgs e) { foreach( string item in myList ) { AddItem(item); } } }