Solved

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

Posted on 2009-04-09
8
331 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
Comment Utility
Here is a sample of threading
http://www.yoda.arachsys.com/csharp/threads/
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility

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
Comment Utility
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
Comment Utility
@gauthampj

It compiles and runs but the combo box never gets populated
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
Excellent!  Thanks for all the help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

7 Experts available now in Live!

Get 1:1 Help Now