Search code examples
c#winformsuser-controls

Usercontrol in windows Form closed when my BackgroundWorker is running


I made a form with 3 usercontrols and display it with button. In 2 of them I got a form for make a backup SQL and a restore. For the backup my usercontrol get closed after the back is complete. For the restore I run a background Worker and my usercontrol get closed in the middle of the process.

There is no error so I don't know what to do.

I try nothing because i have no error

I add my user-control in my form with this

if (!Instance.PanelContainer.Controls.ContainsKey("Backup"))
{
    Backup backup = new Backup();
    backup.Dock = DockStyle.Fill;
    PanelTools.Controls.Add(backup);
}
Instance.PanelContainer.Controls["Backup"].BringToFront();

Then I just have a button that run a SQL Query

BACKUP DATABASE DBPDM
TO DISK = 'PathBackupNameBackup'
WITH
  FORMAT,
  MEDIANAME = 'SQLServerBackups',
  NAME = 'Full Backup of SQLTestDB';

And the crash is at the end of BAK

UPDATE :

I find the issue with the restore UserControl. It's because I kill explorer.exe.

Usercontrol is an explorer.exe ? Can I kill explorer without that close my usercontrol ?


Solution

  • Update of my issue. My Usercontrol doesn't get close but like I said I've got 3 usercontrol. When my app is starting I load the usercontrol 1. With a button I load a usercontrol 2.

    When the explorer.exe get kill the usercontrol 1 is bringback to front. I see it's because I load this one on a PaintEvent like that :

    private void PanelTools_Paint(object sender, PaintEventArgs e)
        {
            _obj = this;
            if (!Instance.PanelContainer.Controls.ContainsKey("Settings"))
            {
                Settings settings = new Settings();
                settings.Dock = DockStyle.Fill;
                PanelTools.Controls.Add(settings);
            }
            Instance.PanelContainer.Controls["Settings"].BringToFront();
        }
    

    to fix it I just load my usercontrol 1 on the LoadEvent :

     private void SupportTools_Load(object sender, EventArgs e)
        {
            _obj = this;
            if (!Instance.PanelContainer.Controls.ContainsKey("Settings"))
            {
                Settings settings = new Settings();
                settings.Dock = DockStyle.Fill;
                PanelTools.Controls.Add(settings);
            }
            Instance.PanelContainer.Controls["Settings"].BringToFront();
        }
    

    Thanks a lot for your time all and sorry for not being clear when i explain my issue.