2

Take a look at my code.

Thread[] connect_thread;
public void thread_runned()
{
    connect_thread = new Thread[dataGridView1.SelectedRows.Count];
    for (int index = 0; index < dataGridView1.SelectedRows.Count; index++)
    {
        connect_thread[index] = new Thread(new ThreadStart(connect));
        connect_thread[index].Start();
    }
}
public void connect()
{
   //performance code here
}
public void ButtonClick1()
{
    //User select rows 0-4
    thread_runned();
}

public void ButtonClick2()
{
    //User select rows 5-9
    thread_runned();
}

According to the code above, when I run it, and click on ButtonClick1 and ButtonClick2, it returns two different connect_threads (see this debug for more detail.)

//Debug when ButtonClick1 is running
connect_thread = array(
    [0] = System.Threading.Thread
    [1] = System.Threading.Thread
    [2] = System.Threading.Thread
    [3] = System.Threading.Thread
)

//Debug when ButtonClick2 is running
connect_thread = Error: Index was outside the bounds of the array.

Now, I want to add a new thread item into this thread array, but the indeces must continue like the old thread items (i.e, the next indeces will be [4], [5], [6], etc.)

I'm not worrying about this error:

//Debug when ButtonClick2 is running
connect_thread = Error: Index was outside the bounds of the array.

because I can create a list of threads using dataGridView1.Rows.Count, and it will work fine. However, I'm looking to do it the other way other because when the user adds more data into dataGridView, the index will be wrong again.

How can I append new threads to the end of my thread array while preserving indeces?

ASCIIThenANSI
  • 865
  • 1
  • 9
  • 27
Viet Nguyen
  • 2,285
  • 2
  • 26
  • 43

3 Answers3

3

Just try to replace your array with list like below:

List<Thread> connect_thread = new List<Thread>()
public void thread_runned()
{
    for (int index = 0; index < dataGridView1.SelectedRows.Count; index++)
    {
        connect_thread.add(new Thread(new ThreadStart(connect)));
        connect_thread[connect_thread.Count-1].Start();
    }
}
Oleh Dokuka
  • 11,613
  • 5
  • 40
  • 65
3

Since you have an array with a defined size, it is not a good pratice define a new size for an array. Instead it, you could use a List<Thread> and add how many threads you need, for sample:

List<Thread> connect_threads;

public void thread_runned()
{
    int total =  dataGridView1.SelectedRows.Count;

    // define the array
    connect_threads = new List<Thread>();   

    // define threads all threads on the list
    for (int index = 0; index < total; index++)
        connect_threads.Add(new Thread(new ThreadStart(connect)));

    // start all threads on the list
    foreach(var thread in connect_threads)
        thread.Start();

    // if you want to wait all the threads on the list to finish
    foreach(var thread in connect_threads)
        thread.Join();              
}
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
1

For the sake of argument, use ThreadPool.

    Int32 _ActiveThreadCount = 0;

    delegate void SetTextCallback(String text);

    public Int32 ActiveThreadCount
    {
        get 
        { 
            return _ActiveThreadCount; 
        }
        set 
        {
            _ActiveThreadCount = value;
            SetText(value.ToString());
        }
    }

    private void OnThreadCallback(Object state)
    {
        System.Threading.Thread.Sleep(1000);
        ActiveThreadCount--;
    }

    private void SetText(String text)
    {
        if (label2.InvokeRequired)
        {
            SetTextCallback callback = new SetTextCallback(SetText);
            this.Invoke(callback, text);
        }
        else
        {
            this.label2.Text = text;
        }
    }

Add threads using

System.Threading.ThreadPool.QueueUserWorkItem(
    new System.Threading.WaitCallback(OnThreadCallback), null);
ActiveThreadCount++;

As for the rest, I'm not really sure what exactly the question is here.

David Carrigan
  • 751
  • 1
  • 8
  • 21