views:

45

answers:

1

I've got a thread that currently updates a DataGridView on one tab (STATUS) of a TabControl. This thread currently functions quite well. What I would like to do is be able to update a small collection of controls on a different tab (SYNCH.) if the button to run the thread is clicked while on the SYNCH. tab. I considered passing an integer into the thread at Thread.Start(), but then I'm not sure of how to change the int parameter, which in this case is the TabControl.SelectedIndex property, after the thread has been started. This is an issue because switching tabs suspends the thread, but doesn't permit a new tab index to be passed in.

I can probably break this functionality out into a different thread that I would start only when on the SYNCH. tab, but all of the device query functionality is already handled in the existing thread. I'm sort of averse to duplicating functionality for a quick workaround.

Below is the function that I schemed up to "solve" the issue, but it's ineffective because the tabIndex variable is set at Thread.Start and I can't currently change it. Is there a way to resolve this?

void writeTable(int tabIndex)
    {
        if (InvokeRequired)
        {
            BeginInvoke(new ctrlStatDel(writeTable), tabIndex);
            return;
        }

        switch((appTabs)tabIndex)
        {
            case appTabs.STATUS:
                int i4 = 0;

                ctrlProgBar.Value = 0;

                for (int i2 = 0; i2 < 4; i2++)
                {
                    for (int i3 = 0; i3 < 10; i3++)
                    {
                        statusDGV.CurrentCell = statusDGV[i2, i3];
                        statusDGV.CurrentCell.Value = colFields[i4];
                        i4++;
                    }
                    ctrlProgBar.PerformStep();
                }

                statusDGV.CurrentCell = null;

                break;

            case appTabs.SYNCH:
                for (int idx = 0; idx < statFields.Count; idx++)
                    statControls[idx].Text = statFields[idx];
                break;
        }   
    }
+1  A: 

What you seem to need is to run a different method on a thread when another tab is selected. Make the choice when (before) starting the Thread, not in the Thread.

Unless you want it to update the DataGrid and the controls (I didn't read that, but it seems plausible). In that case you can pass a simple boolean as parameter (int the object state parameter).

You didn't post the Tread starting code, which is the most relevant.

Henk Holterman