Modifying VB.NET Console app to "pump messages"

I have a long running Console application which increases memory demand in a section which reuses the same class instantiation several thousand times.

The class pulls LDAP information from two different sources and then does some compares/contrasts.  The resulting values are obtained through Public properties.

So the main loop instantiates the class once with NEW and then starts the several thousand loop iteration.  The LDAP connection information is passed once as class properties before the loop starts.
Each loop iteration uses a .Reset Method to reset certain LDAP obtain values to a known default.  

From the increasing memory demand and the "The CLR has been unable to transition from COM context" error message obtain while running in debug mode, I would guess that the console app is unable to process some background messages.

There is no Windows Form and so the the System.Windows.Forms.Application.DoEvents method is unavailable.

What I've done so far:
1. Reviewed LDAP module code and reduced variable declarations to bare minimum.
2. In the main module,  use     <MTAThread()> Public Sub Main()
3. Set a timer which kicks off every 15 seconds.   The handler does this:
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime)
I know that the GC code is bad practice but I am feeling rather desparate.

Thank you for all hints and assistance.

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.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Hi Richard,

Are you open to using a WinForms project instead of a Console?

It's possible to Inherit a Class from ApplicationContext() and pass that to Application.Run() resulting in an app that has NO Forms but still has a message pump.

Read the two posts I made here for more info:

Let me know if you want more examples...

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
RichardKlineAuthor Commented:
Looks interesting....  Is there a preference as to which solution is "better"

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Not really.  Since you're writing a non-standard app I'd go with "whatever works".  =)
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

RichardKlineAuthor Commented:
*sigh* that seems to be the story of my life -- working with non-standards.

I'm sure that this will work -- will let you know.


Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Haha...good luck!
RichardKlineAuthor Commented:
Question:  This code still allows a window to be created.   Any thoughts on what I am missing?
Thank you.

Imports Microsoft.VisualBasic.ApplicationServices

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    ' Declaration
    Public Event Startup(ByVal sender As Object, ByVal e As StartupEventArgs)

    ' Usage
    Public Sub Form1_Startup(ByVal sender As Object, ByVal e As StartupEventArgs ) Handles Me.Startup
        e.Cancel = True
    End Sub
End Class
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If you want NO forms at all...
(1) Start with a standard WinForms project
(2) Add a MODULE with a Public Sub Main():

    Module Module1

        Public Sub Main()

        End Sub

    End Module

(3) Click on Project --> Properties and UNCHECK the "Enable Application Framework" option.
(4) Change the "Startup Object" to "Sub Main"
(5) Right click Form1.vb and Delete it.
(6) Now add in your ApplicationContext:
Module Module1

    Public Sub Main()
        Application.Run(New MyContext)
    End Sub

End Module

Public Class MyContext
    Inherits ApplicationContext

    Public Sub New()
        ' this is the entry point
    End Sub

    Private Sub Worker()
        ' do stuff in here....etc...

        ' since we have a message pump the app will NOT die when this method exits
        ' you can create other threads and trap events etc...

        ' end the app by calling this from somewhere:
    End Sub

End Class

Open in new window

RichardKlineAuthor Commented:
Excellent Help!

The memory consumption is now reasonable.    Still getting a ContextSwitching Error but I feel more comfortable in disabling that warning.

To anyone who reads this later, please note that this method while effective does disable the console app display.   The console.writeline instructions execute but the output is apparently thrown into the bit bucket.  Might be tempted to make this a real windows form and write text to a textbox.
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
.NET Programming

From novice to tech pro — start learning today.