Link to home
Start Free TrialLog in
Avatar of phobis
phobis

asked on

C# Application Thread is running away

I think I have some run away threads...

I am creating threads, like so (for a custom MessageBox that I have made):

Thread newThread = new Thread(new ThreadStart(this.threadMethod));
newThread.Start();


Now my application's memory is constantly growing... and will not stop...

Is there a way to clean up Threads that are finished running?
Avatar of thetool
thetool

When the thread is finished you can set newThread=null; and see if that helps any.
try this one

newThread.Dispose(); //I think it should help
SPUH dispose is not a method for threads.  Unless I am missing something somewhere along the way.
Yes, thats true, I was wrong. May be this one disposes the Thread: newThread.Abort();
Doe ThreadMethod() ever exit?
newThread.Abort() "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread."  

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemThreadingThreadClassAbortTopic.asp

This is not the graceful way to take care of this.

When a thread is finished executing it should eventually be collected by the Garbage Collector.  However, one never knows when the GC will be called.

Also, what are you doing within the code on the new thread?

-Justin
Check this little tidbit out... requires less scanning than msdn.

http://www.c-sharpcorner.com/2/mt_beginner1.asp

-Justin
Do you perchance use a timer that never gets disabled?
ASKER CERTIFIED SOLUTION
Avatar of billtouch
billtouch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial