c# WPF busyindicator for new window

384 views Asked by At

Currently i am having a WPF function that opens a new window, that looks like that:

var sqlConnectionSetting = new SqlConnectionSettingsWnd
{
    Owner = this
};
sqlConnectionSetting.Show();

The problem is that inside this window I have a cycle that looks for available SQL instances:

string myServer = Environment.MachineName;

DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
    if (myServer == servers.Rows[i]["ServerName"].ToString()) ///// used to get the servers in the local machine////
    {
        if ((servers.Rows[i]["InstanceName"] as string) != null)
            localSqlServer.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
        else
            localSqlServer.Items.Add(servers.Rows[i]["ServerName"]);
    }
}      

and the window takes long time to open. I wanted to use the BusyIndicator to display some message like "searching for SQL servers" or something like that, but I cannot find any way to do it (I never programmed XAML before....) can someone please help me with that task? :)

1

There are 1 answers

0
Hagay Goshen On

I assume you are not using MVVM with your code in WPF.

You could put some panel in the center of your window with some static text for start (then you can research animation in WPF) which is shown on start and when your lengthy code ends you hide it.

please note that your GUI will not respond while your code is running on the same thread of the UI.