Pass method name as a param?  ThreadStart

Tom Knowlton
Tom Knowlton used Ask the Experts™
on
The code below does the following:

1)  Shows a windows form with a progressbar with style set to marquee (continuously runs).
2)  Creates and starts a thread which runs a lengthy process
3)  While that thread is alive I call DoEvents() so the progress bar form will refresh
4)  Lengthy process finishes, I display a message box.


My actual question is -- is there a way to make

            ThreadStart method = new ThreadStart(this.RunSQLAndDisplayResults);

more generic?  For example, pass in the method call in the ThreadStart constructor as a parameter?

Something like:  


private void RunSQL_Click(<method name or signature>)
{
...
...

            ThreadStart method = new ThreadStart(<method name or signature>);

...
...
}




private void buttonRunSQL_Click(object sender, EventArgs e)
        {
            WaitDialog wd = new WaitDialog();
            wd.Show();
            ThreadStart method = new ThreadStart(this.RunSQLAndDisplayResults);
            Thread thread = new Thread(method);
            thread.Start();

            while (thread.IsAlive)
            {
                System.Windows.Forms.Application.DoEvents();                
            }

            this.dataGridViewDataToExport.SuspendLayout();
            this.dataGridViewDataToExport.Columns.Clear();
            this.dataGridViewDataToExport.DataSource = this._sqldataset.Tables[0];
            this.dataGridViewDataToExport.ResumeLayout();

            labelCountOfItemsToExport.Text = "Row Count:  " + this._sqldataset.Tables[0].Rows.Count.ToString();

            MessageBox.Show("Data has been selected based on your preferences and is ready for export");

            wd.Hide();

        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Web developer
Commented:
My current solution:
ThreadStart method = new ThreadStart(this.ExportDataToFile);
            ShowWaitDialog(method, "Export file has been created.", true); 
 
 
...
...
 
 
...
...
 
 
private void ShowWaitDialog(ThreadStart ns, string DoneMessage, bool showdonemessage)
        {
            WaitDialog wd = new WaitDialog();
            wd.Show();            
            ThreadStart method = new ThreadStart(ns);            
            Thread thread = new Thread(method);
            thread.Start();
 
            while (thread.IsAlive)
            {
                System.Windows.Forms.Application.DoEvents();
            }
 
            wd.Hide();
 
            if (showdonemessage)
            {
                MessageBox.Show(DoneMessage);
            }
        }

Open in new window

Tom KnowltonWeb developer

Author

Commented:
Any takers?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial