2

In one of my code, I used a public static object of a form. In this code I have used Show() and Hide() function on this form, because I don't want to close this form as long as the main application is running. Now if I close the form from "Task manager - > Application Tab" this form gets disposed. I have function like the following:

public static fullScreen = null;

public FormFullScreen GetBackFullScreen()
{
if(fullScreen == null)
{
fullScreen = new fullScreen();
}

return fullScreen;
}

Now when I call "GetBackFullScreen().Show()", I get can not show Disposed Object form. Can anybody suggest a solution? Thanks in advance.

Mehbube Arman
  • 472
  • 1
  • 7
  • 18
  • 1
    an object cannot be null, but the reference can be. – Victor Mukherjee Jun 23 '13 at 11:26
  • Why are you disposing of the form if you intend to re-use it? Either dispose and set the reference to null, or do not dispose of the form and re-use the same instance – Lukazoid Jun 23 '13 at 11:28
  • Could you show us how you dispose of your form? Where the dispose method is invoked – Lukazoid Jun 23 '13 at 11:30
  • @ Lukazoid, I was testing what happens if I close it from Task Manager -> Application tab . – Mehbube Arman Jun 23 '13 at 11:30
  • If you close it from task manager, I believe you are ending the the entire process, not just that form. I am probably misunderstanding what is going on here. – Lukazoid Jun 23 '13 at 11:32
  • @Lukazoid, I have a main form and a fullScreen form. I closing the fullScreen form from the TaskManager not the main form. – Mehbube Arman Jun 23 '13 at 11:34

2 Answers2

3
public static fullScreen = null;

public FormFullScreen GetBackFullScreen()
{
if(fullScreen == null || fullScreen.IsDisposed)
{
    fullScreen = new fullScreen();
}

return fullScreen;
}

Get if the form is disposed, if so, create a new instance.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isdisposed.aspx

Oscar
  • 13,594
  • 8
  • 47
  • 75
  • I tried this way, but it seems every time I try to open this form after dispose, there is an increase in memory..Could U Suggest another way ? – Mehbube Arman Jun 23 '13 at 11:29
  • Increase in memory? It's normal, you're creating a new object. Is it unaffordable or what? – Oscar Jun 23 '13 at 11:36
  • Sorry Sir, I have to find a way so that memory does not increase. – Mehbube Arman Jun 23 '13 at 11:38
  • 2
    The disposed object stays in memory until garbage collection. You may try to manually trigger garbage collection if you don't want to increase memory usage. – Johannes Egger Jun 23 '13 at 11:44
0

I do not entirely understand the problem here, but maybe you could try the following:

public static fullScreen = null;

public FormFullScreen GetBackFullScreen()
{
    if(fullScreen == null)
    {
        fullScreen = new fullScreen();
        fullScreen.Closed += (s, e) => fullScreen = null;
    }
    return fullScreen;
}

This will ensure that whenever the form is closed, the backing field is cleared, and thus a new form is subsequently created.

Lukazoid
  • 19,016
  • 3
  • 62
  • 85
  • explicit null assignment is not a good practice(as suggested by Hans Passant at http://stackoverflow.com/questions/17130382/understanding-garbage-collection-in-net ) – Victor Mukherjee Jun 23 '13 at 11:40
  • and it does not solve my problem as well, memory is still increasing. – Mehbube Arman Jun 23 '13 at 11:41
  • @VictorMukherjee Hans Passant is saying that assigning the null to a local variable to encourage GC is a pointless practice if the variable is no longer referenced from that point onwards (as the GC will happily collect it anyway). Here I am assigning null to a static field, which is always referenced. I do not believe the post by Hans is applicable to this situation. – Lukazoid Jun 23 '13 at 11:46
  • I think the solution suggested by Oscar just serves the purpose. Whenever a new object is assigned to fullScreen, the old one will be collected by GC in due time. Assigning null on form.Closed may not be necessary. – Victor Mukherjee Jun 23 '13 at 11:53
  • @Lukazoid --> I know there will be a solution that I could not find by now. So I have added FormClosing Event for fullScreen and Set e.Cancel = true. Now the form only closes if the mainForm is closed as well. But thanks to all of you for your time. And if find a better suggestion please add them here. Thanks. – Mehbube Arman Jun 23 '13 at 12:11
  • @VictorMukherjee I agree Victor, but if `fullScreen` consumes a lot of memory, having an unused reference sitting around to it seems like a waste. But yes, both achieve the same purpose. – Lukazoid Jun 23 '13 at 14:57