locked
Memory keeps increase with every call to a method with timer!! RRS feed

  • Question

  • namespace WindowsFormsApplication7
    {
              public partial class Form1 : Form
              {
                              public bool aa;

                      public Form1()
                   {
                              InitializeComponent();
                    }
                              private void Form1_Load(object sender, EventArgs e)
                              {
                                 timer1.Interval = 10000;
                                 timer1.Start();
                              }
                              void monitors()
                             {
                                   if (Screen.AllScreens.Length > 1)
                                   {
                                          Form2 frm = new Form2();
                                                 if (aa == false)
                                                   {
                                                        if (frm.IsOpen == false)
                                                   {
                                                           frm = new Form2();
                                                           frm.Show(); frm.IsOpen = true;
                                                           aa = true;
                                                   }
                                                 else
                                                  {
                                                      frm.Activate(); frm.WindowState = FormWindowState.Normal;
                                                   }
                                                }
                                            }
                                         }
                                                public static Screen GetSecondaryScreen()
                                                {
                                                      if (Screen.AllScreens.Length == 1)
                                                {
                                                      return null;
                                                }
                                                        foreach (Screen screen in Screen.AllScreens)
                                                         {
                                                              if (screen.Primary == false)
                                                               {
                                                                       return screen;
                                                                }
                                                          }
                                                       return null;
                                                 }
                                                     private void timer1_Tick(object sender, EventArgs e)
                                                      {
                                                            monitors();
                                                       }
                                                     }
                                                }
    --------------------------------------------------------
    Ok My problam is memory keeps increase with every call to a method. this program doing if 2nd monitor pluged in to computer its opening form2 automaticly.this timer for checking monitors 2nd is still plugin or not . if in ...... if out ..... just i want to detect all the time.

    i dont know i am doing right or not but i want to a program working at the back dont show any form. when i plugin 2nd monitor it will open form2 when i unplug second monitor its close form2 and still runing at the back. if i re plug in 2nd monitor its will again open form2.

    this code is working good for me i setup this program starting at windows startup and its working good but just a big problam is when timer tick every 10 sec memory goes up and half an hour program gives error because of memory full how can i fix it. or are there any detect monitors code working like this? Thank you for interesting....


    • Edited by qqallstars Thursday, September 8, 2011 8:31 PM
    Thursday, September 8, 2011 8:21 PM

Answers

  • The problem appears to be related to when you have the second monitor plugged in, you always create a new instance of the form, so you never know if the last one was opened or not. Here is a slightly modified version:

    namespace WindowsFormsApplication7 {
    public partial class Form1 : Form {
    public bool SecondFormOpen;
    public Form2 SecondaryForm;
     
    public Form1() {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e) {
    SecondaryForm = new Form2();
    timer1.Interval = 10000;
    timer1.Start();
    }
    void monitors() {
    if(Screen.AllScreens.Length > 1) {
    if(SecondFormOpen == false) {
    //Show the secondary form
    if(SecondaryForm.IsOpen == false) {
    SecondaryForm.Show();
    SecondaryForm.IsOpen = true;
     
    } else {
    SecondaryForm.Activate();
    SecondaryForm.WindowState = FormWindowState.Normal;
    }
    SecondFormOpen = true;
    }
    } else {
    //Hide the secondary form
    SecondaryForm.Hide();
    SecondaryForm.IsOpen = false;
    SecondFormOpen = false;
    }
     
    }
    public static Screen GetSecondaryScreen() {
    if(Screen.AllScreens.Length == 1) {
    return null;
    }
    foreach(Screen screen in Screen.AllScreens) {
    if(screen.Primary == false) {
    return screen;
    }
    }
    return null;
    }
    private void timer1_Tick(object sender, EventArgs e) {
    monitors();
    }
    }
    }

    As you can see, I made Form2 a member object so it is only ever instantiated once. This should fix your memory leak.

    I also added an else clause to your check for how many screens you have on. If you have only 1 screen, then make sure the Form2 is hidden.


    For readability, I also renamed that 'aa' variable to something more meaningful 'SecondFormOpen'
    • Edited by Shadowfoxish Thursday, September 8, 2011 10:10 PM
    • Marked as answer by qqallstars Friday, September 9, 2011 6:45 PM
    Thursday, September 8, 2011 10:09 PM

All replies

  • The problem appears to be related to when you have the second monitor plugged in, you always create a new instance of the form, so you never know if the last one was opened or not. Here is a slightly modified version:

    namespace WindowsFormsApplication7 {
    public partial class Form1 : Form {
    public bool SecondFormOpen;
    public Form2 SecondaryForm;
     
    public Form1() {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e) {
    SecondaryForm = new Form2();
    timer1.Interval = 10000;
    timer1.Start();
    }
    void monitors() {
    if(Screen.AllScreens.Length > 1) {
    if(SecondFormOpen == false) {
    //Show the secondary form
    if(SecondaryForm.IsOpen == false) {
    SecondaryForm.Show();
    SecondaryForm.IsOpen = true;
     
    } else {
    SecondaryForm.Activate();
    SecondaryForm.WindowState = FormWindowState.Normal;
    }
    SecondFormOpen = true;
    }
    } else {
    //Hide the secondary form
    SecondaryForm.Hide();
    SecondaryForm.IsOpen = false;
    SecondFormOpen = false;
    }
     
    }
    public static Screen GetSecondaryScreen() {
    if(Screen.AllScreens.Length == 1) {
    return null;
    }
    foreach(Screen screen in Screen.AllScreens) {
    if(screen.Primary == false) {
    return screen;
    }
    }
    return null;
    }
    private void timer1_Tick(object sender, EventArgs e) {
    monitors();
    }
    }
    }

    As you can see, I made Form2 a member object so it is only ever instantiated once. This should fix your memory leak.

    I also added an else clause to your check for how many screens you have on. If you have only 1 screen, then make sure the Form2 is hidden.


    For readability, I also renamed that 'aa' variable to something more meaningful 'SecondFormOpen'
    • Edited by Shadowfoxish Thursday, September 8, 2011 10:10 PM
    • Marked as answer by qqallstars Friday, September 9, 2011 6:45 PM
    Thursday, September 8, 2011 10:09 PM
  • You are creating new instance of Form2 in monitors() function. You should manage, creation of object using Singleton pattern. It will definately decrease your memory.

    Another reason will be there might be some unmanged resources which are not freeing. If that is the case, Please implement IDisposable interface and dispose all unmanged resources in Dispose() method. As per my information, Windows Form implements IDisposable itself.

    Hope this helps.


    Pravin Arote, MCTS - Web Technologies If reply answers your question, Please mark as Answer :)
    Friday, September 9, 2011 6:44 AM
  • Barrakoda
    thank you for this codes.
    Friday, September 9, 2011 7:38 PM