Open a modal form in a separate thread and then close it

0

When starting my application, I open a modal form in a new thread through a background worker, while data from a SQL Server database is brought in the main thread.

When the process of bringing that data ends, the main form of the application is displayed

PROBLEM: I can not close the modal form.

CODE:

public partial class frmPrincipal : Form
{

    List<string> _nodosMarcados = new List<string>();
    bool _interrumpir = false;
    BackgroundWorker worker; // Este es el bw que utilizo para el hilo nuevo
    frmIniciando _modalInicio = new frmIniciando(); // Este es el formulario modal



    public frmPrincipal()
    {
        InitializeComponent();

        worker = new BackgroundWorker();
        worker.WorkerSupportsCancellation = true;
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_StopWorking;
        worker.RunWorkerAsync();
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        _modalInicio.ShowDialog();
    }

    // En el Load del Form principal va toda la lógica para traer los datos de la BD, 
    // y al final intento cerrar el form modal con _modalInicio.Close(), pero no se cierra

EDIT:

I add a clarification about what I want to do: the dialog box at the beginning shows a message "Loading application ..", what I want is that when I finish loading the main form, that box is closed.

Why am I doing it in a new thread of execution? Because the main thread is busy bringing the info from the DB.

Why do I use ShowDialog instead of Show to show the box? Because with Show he does not show it to me. He loads it but it remains invisible and when it finishes loading the main one disappears but I never see it on the screen. With ShowDialog I see it correctly but I do not know how to close it.

    
asked by Willy616 14.07.2017 в 21:16
source

1 answer

0

I solved it in the following way (in case someone serves):

Instead of using a background worker, I used threads:

ThreadStart proceso = new ThreadStart(MostrarCuadroInicio);
Thread hilo = new Thread(proceso);
hilo.Start();

// CODIGO QUE SE EJECUTA MIENTRAS MUESTRO EL CUADRO MODAL

hilo.Abort();

The "ShowCheckStart" method does the following:

private void MostrarCuadroInicio()
{
    frmIniciando _modalInicio = new frmIniciando("Iniciando aplicación ...", true);
    _modalInicio.ShowDialog();
}
    
answered by 17.07.2017 / 17:15
source