Why DataConnectionDialog window are different depending on thread it running?

252 Views Asked by At

I got an WPF form with DataConnectionDialog (from Microsoft.Data.ConnectionUI)

And the thing is in case of it called from the same thread as UI it looks like this:

enter image description here

Code is here:

private void btnSetDbConnection_OnClick(object sender, RoutedEventArgs e)
{
    string connectionString = null;

    try
    {
        //await Task.Run(() => 
        //{
              DataConnectionDialog dataConnectionDialog = new DataConnectionDialog();                  Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dataConnectionDialog);
              dataConnectionDialog.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.SqlDataSource;
              dataConnectionDialog.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider;

              if (DataConnectionDialog.Show(dataConnectionDialog) == System.Windows.Forms.DialogResult.OK)
              {
                _dataHandler.InitializeConnection(dataConnectionDialog.ConnectionString);
              }
          //}).ConfigureAwait(false);                
      }
      catch (Exception ex)
      {
          throw ex;
      }
  }

And if not - like that:

enter image description here

Code (well, it's the same but uncommented):

private async void btnSetDbConnection_OnClick(object sender, RoutedEventArgs e)
{
    string connectionString = null;

    try
    {
        await Task.Run(() =>
        {
            DataConnectionDialog dataConnectionDialog = new DataConnectionDialog();
            Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dataConnectionDialog);
            dataConnectionDialog.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.SqlDataSource;
            dataConnectionDialog.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider;

            if (DataConnectionDialog.Show(dataConnectionDialog) == System.Windows.Forms.DialogResult.OK)
            {
                _dataHandler.InitializeConnection(dataConnectionDialog.ConnectionString);
            }
        }).ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Wondering why is so?

0

There are 0 best solutions below