GC continues to grow how can I clean it up?

I have the following code and Gen#0 GC continues to grow. I thought adding gc.collect into the code would help but it doesn't. What code do I need to add to get the GC to stop growing?

int i = 0;

            try
            {
                while (i < 200000)
                {
                    tEditStart = DateTime.Now;
                    IAsyncResult iar = dlEdit.BeginInvoke(5, out threadId, null, null);

                    while (iar.IsCompleted == false)
                    {
                        Application.DoEvents();
                    }

                    int iDllReturn = dlEdit.EndInvoke(out threadId, iar);

                    tEditFinish = DateTime.Now;

                    tsEdit = tEditFinish - tEditStart;
                    lNbrSec = tsEdit.Milliseconds;
                    i++;
                    textBox1.Text = "DLL Result: " + iDllReturn + " - Call# " + i + " took " + lNbrSec.ToString();
               
                    GC.Collect(0);
                }
            }
LVL 2
sutoriusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BlackTigerXCommented:
this is killing you
textBox1.Text = "DLL Result: " + iDllReturn + " - Call# " + i + " took " + lNbrSec.ToString();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sutoriusAuthor Commented:
How do you recommend output the DLL Result?
BlackTigerXCommented:
do you have to display that 200,000 times?

first, move it out of the while loop to see if that helps

and remove the GC.Collect();
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

sutoriusAuthor Commented:
With each call to the DLL I need to see the result, even if it is for a blip of a second.

That solved part of the problem. The .NET Framework class Control.MultithreadSafeCallScope is no longer growing but another one is, WindowsFormsSynchronizationContext.

I am a web guy not a windows guy so I don't understand how you are penalized for having a textbox on a form in a while loop as a memory leak (undisposed instances). Can you explain?
BlackTigerXCommented:
the problem there is that you are allocating 200,000 strings, strings are immutable objects, which means you cannot change their contents (in managed code), everytime you assign a string, you are actually trowing away the current one, requesting memory for the new string, and assigning it's value
BlackTigerXCommented:
also, this code

while (iar.IsCompleted == false)
                    {
                        Application.DoEvents();
                    }

is not a very good idea, you should put a Sleep in between, at least

while (iar.IsCompleted == false)
{
  Thread.Sleep(30);
  Application.DoEvents();
}
sutoriusAuthor Commented:
Thanks for your assistance.
BlackTigerXCommented:
and even better than that, you should use functions to wait for signals instead of that loop, I don't know exactly what objects you are using, so I can't be more specific
sutoriusAuthor Commented:
Can you be specific now with your idea on using functions instead of a loop?

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public delegate int AsyncDelegate(int callType, out int threadId);
        int threadId;
        string sProcessMsg = "";
        //string strWaiting = "Waiting";
        //long lNbrMin = 0;
        long lNbrSec = 0;
        DateTime tEditStart;
        DateTime tEditFinish;
        TimeSpan tsEdit;
        int iDllReturn;

        public Form1()
        {

            InitializeComponent();
           
            //this.Controls.Add(this.textBox1);

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            AsyncCalls();
        }

        public void AsyncCalls ()
        {
            // Create an instance and delegate of the Edit DLL.  
            EditCall cEdit = new EditCall();
            AsyncDelegate dlEdit = new AsyncDelegate(cEdit.CallHCFA);

            int i = 0;

            try
            {
                while (i < 200000)
                {
                    tEditStart = DateTime.Now;
                    IAsyncResult iar = dlEdit.BeginInvoke(5, out threadId, null, null);

                    while (iar.IsCompleted == false)
                    {
                        Application.DoEvents();
                    }

                    iDllReturn = dlEdit.EndInvoke(out threadId, iar);

                    tEditFinish = DateTime.Now;

                    tsEdit = tEditFinish - tEditStart;
                    lNbrSec = tsEdit.Milliseconds;
                    i++;
                }
                //textBox1.Text = "DLL Result: " + iDllReturn + " - Call# " + i + " took " + lNbrSec.ToString();
            }
            catch (Exception f)
            {
                MessageBox.Show("Exception: " + f.Message, sProcessMsg,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (components != null)
                {
                    components.Dispose();
                }
                base.Dispose();
            }
        }
    }

   

    public class EditCall
    {
        [DllImport("Ecghcfa.dll", EntryPoint = "ECgHCFA")]
        public extern static int ECgHCFA(int lEditNo, string sParams, string sServer, string sDatabase, string sUsername, string sPassword);
        // The method to be executed asynchronously.
        public int CallHCFA(int callType, out int iReturn)
        {
            if (callType == 3)
                try
                {
                iReturn = ECgHCFA(3, "", "cltecdev5\\audit", "ca101web", "Install", "LiveStrong");
                }
                catch (Exception f)
                {
                    MessageBox.Show("Message " + f);
                }
            else
                try
                {
                    iReturn = ECgHCFA(5, "", "cltecdev5\\audit", "ca101web", "Install", "LiveStrong");
                }
                catch(Exception f)
                {
                    MessageBox.Show("Message " + f);
                }
            iReturn = 0;
            return (iReturn);
        }
    }
}
BlackTigerXCommented:
first of all, why are you even using asynchronous methods, if you're waiting for it to be done before you proceed anyway?

here's some interesting reading
http://www.codeproject.com/csharp/begininvoke.asp
http://www.codeproject.com/csharp/workerthread.asp?df=100&forumid=2441&exp=0&select=1178892
sutoriusAuthor Commented:
Thanks for the articles. Very helpful. You make a good point about using asynch.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.