Solved

Retreiving a list of available SQL Servers in a new thread.

Posted on 2009-04-09
8
333 Views
Last Modified: 2013-12-17
I use the snippet listed below to get a list of available SQL Server instances.  I would like to find an example of how to execute this in a separate thread so as not have the application appear "frozen".  

Thanks!
DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

ListServers.DisplayMember = "name";

ListServers.ValueMember = "name";

ListServers.DataSource = dt.DefaultView;

Open in new window

0
Comment
Question by:andy_ee
  • 3
  • 3
  • 2
8 Comments
 
LVL 7

Expert Comment

by:AUmidh
ID: 24107185
Here is a sample of threading
http://www.yoda.arachsys.com/csharp/threads/
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24107186

System.Threading.Thread.Start

(

new ThreadStart

(delegate

{

DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

ListServers.DisplayMember = "name";

ListServers.ValueMember = "name";

ListServers.DataSource = dt.DefaultView;
 

}

));

Open in new window

0
 
LVL 7

Expert Comment

by:AUmidh
ID: 24107221
From The Web page....

using System;
using System.Threading;

public class Test
{
    static void Main()
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        thread.Start();
       
        for (int i=0; i < 5; i++)
        {
            Console.WriteLine ("Main thread: {0}", i);
            Thread.Sleep(1000);
        }
    }
   
    static void ThreadJob()
    {
        DataTable dt = SmoApplication.EnumAvailableSqlServers(false);
ListServers.DisplayMember = "name";
ListServers.ValueMember = "name";
ListServers.DataSource = dt.DefaultView;
    }
}

0
 

Author Comment

by:andy_ee
ID: 24113170
@gauthampj

It compiles and runs but the combo box never gets populated
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24113419

System.Threading.Thread.Start

(

new ThreadStart

(delegate

{

DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

ListServers.Invoke(new ThreadStart(delegate

{

ListServers.DisplayMember = "name";

ListServers.ValueMember = "name";

ListServers.DataSource = dt.DefaultView;

}),null);

}

));

Open in new window

0
 

Author Comment

by:andy_ee
ID: 24115963
I get the following error:

An object reference is required for the non-static field, method, or property 'System.Threading.Thread.Start(object)'
public partial class FormSQLServerList : Form

{

    public FormSQLServerList()

    {

        InitializeComponent();

    }

    private void FormSQLServerList_Load(object sender, EventArgs e)

    {

        System.Threading.Thread.Start

        (

        new ThreadStart

        (delegate

        {

            DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

            ListAvailableSQLServers.Invoke(new ThreadStart(delegate

            {

                ListAvailableSQLServers.DisplayMember = "name";

                ListAvailableSQLServers.ValueMember = "name";

                ListAvailableSQLServers.DataSource = dt.DefaultView;

            }), null);

        }

        ));

    }

}

Open in new window

0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 24117035

public partial class FormSQLServerList : Form

{

    public FormSQLServerList()

    {

        InitializeComponent();

    }

    private void FormSQLServerList_Load(object sender, EventArgs e)

    {

        System.Threading.Thread t = new

        System.Threading.Thread

        (

        new ThreadStart

        (delegate

        {

            DataTable dt = SmoApplication.EnumAvailableSqlServers(false);

            ListAvailableSQLServers.Invoke(new ThreadStart(delegate

            {

                ListAvailableSQLServers.DisplayMember = "name";

                ListAvailableSQLServers.ValueMember = "name";

                ListAvailableSQLServers.DataSource = dt.DefaultView;

            }), null);

        }

        ));

        t.Start();

    }

}

Open in new window

0
 

Author Closing Comment

by:andy_ee
ID: 31568524
Excellent!  Thanks for all the help!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now