The form is exposed on Hide ()

I have a C # WinForms application running on the .NET Framework 4.0.

When a user is inactive for a certain period of time, I want him to hide all displayed forms and show an icon in the notification area. When a user clicks on this icon, a login form appears, and if the credentials are valid, they open the exact forms that were opened before.

To do this, I save the list of open forms in objects Listfrom Formand hide them like this. This method is called by Timer :

private void LogOut()
{
    foreach (Form form in Application.OpenForms)
        if (form.Visible)
        {
            GlobalVariables.formList.Add(form);
            form.Hide();
        }
}

When the credentials are verified, I try to make the forms visible again, for example:

//Show the previous forms.
foreach (Form form in GlobalVariables.formList)
    form.Visible = true;

//Clear the forms list.
GlobalVariables.formList.Clear();

MainForm , , , . ( ShowDialog() MainForm), form.Visible = true; :

ObjectDisposedException was unhandled
Cannot access a disposed object

? , , .

, try-catch , , , .

-, , 3 , !

:. , , , , ShowDialog(). Show(), .

Show() , . , .

+4
4

, , . , Microsoft Winforms. , , , . , .

. , ShowDialog() , . , , , , , . , . , , , . , . . , .

, : ", , , !" , Windows, , , . , , , . , winapi.

, Winforms, , . , , , , ShowDialog(), . , , , .

, . , . , - . , , Windows. .

LockWorkStation() .

+4

, , Hide() - . FormClosing.

: ( . .)

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.ShowDialog();
}

private void button2_Click(object sender, EventArgs e)
{
    Hide();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    MessageBox.Show("Closing");
}

, , . : Visible true, . Show().

+2

, .

, . , MSDN, :

, .

, . , :

using (var theForm = new CreateInvoice())
{
    theForm.ShowDialog();

    if (theForm.Updated)
    {
        GetInvoiceStatus();
    }
}

theForm, . , , theForm. , ShowDialog() , , , using, theForm, .

+2

: () , (Model), . , (). -, , -, GDI, , , GDI.

, MVC MVP. .

, : , , . , .

0

Source: https://habr.com/ru/post/1527182/


All Articles