PDA

Click to See Complete Forum and Search --> : [2.0] A problem with threads


proff.hacker
May 9th, 2006, 02:15 PM
Hi there,

I use threads to process something and then to display the result in a treeview.

This code works but the problem it does not add until all threads finishes. I want to keep adding as long as I am in FindFolders()

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;

namespace MyProject
{
public partial class Form1 : Form
{
private delegate void AddNodeToTreeViewDelegate();


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
int j;

for (j = 1; j < 10; j++)
{
ThreadStart starter = new ThreadStart(FindFolders);
Thread t = new Thread(starter);
t.Start();
}
}

private void FindFolders()
{
//assume this is the folder name I want to display in the treeview
string folder_name = "test";

treeView1.Invoke(new AddNodeToTreeViewDelegate(AddNodeToTreeView), new object[] { folder_name });

}

private void AddNodeToTreeView(string fname)
{
TreeNode p = new TreeNode(fname);
treeView1.Nodes.Add(p);
}

}
}