Second thread only when Windows is closed

0

I have following code

private void AbrirConexao(string strConexao)
{
    try
    {
        conexao = new NpgsqlConnection(strConexao);
        conexao.Open();
    }
    catch (Exception)
    {
        ReconectarDB(null, strConexao);
    }
}

public bool ReconectarDB(string strConexao)
{
    bool erroConexao = false;
    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += (obj, ea) =>
    {
        int erro = 0;
        while (erro <= 4)
        {
            Thread.Sleep(1000);
            try
            {
                conexao = new NpgsqlConnection(strConexao);
                conexao.Open();
                erroConexao = false;
                break;
            }
            catch
            {
                erro++;
                erroConexao = true;
            }
        }
    };
    bw.RunWorkerCompleted += (obj, ea) =>
    {
        if (erroConexao)
            DialogReconectando.AlterarTela(ErroConexao.SemConexao);
        else
            DialogReconectando.Close();
    };
    bw.RunWorkerAsync();

    Application.Current.Dispatcher.Invoke(new Action(() =>
    {
        if (DialogReconectando == null || Conexao.DialogReconectando.IsLoaded == false)
            DialogReconectando = new   DialogErroConexao(ErroConexao.Reconectando);

        if(DialogReconectando.ShowActivated)
        {
            DialogReconectando.ShowActivated = true;
            DialogReconectando.ShowDialog();
        }
    }));

    return erroConexao;
}

I am using the Open Connection method to connect to the database. and when the connection fails, ReconnectDB is trying to reconnect with the database. If it fails, a Window is opened that there are two buttons, Retry and Abort the System.

The problem is that there are situations that I use other concurrent threads that are requesting with the database. In those cases, I would not want it to display a new Window. So if there is a Window open, I would like the Thread to lock until the Window is closed. I tried to solve the problem using EventWaitHandle. However, Window is also caught in this situation. Would you have any idea how you could solve this problem?

    
asked by anonymous 13.05.2018 / 00:50

0 answers