Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
I have created three separate vb.net services that have had some issues on restart of the server they reside on, so in order to handle these issues I've had to put a testing loop into the code for connection to the SQL server. Of course this testing loop caused the services to show "Service Hung on start...", so in order to solve this problem I created a thread in the OnStart method and then used that thread with an address of function to run the code, thus allowing the service to start in a timely manner while still executing the code (testing the sql connection and then performing its startup duties). After implementing these services I mentioned my solution to a friend, who promptly asked me if I was destroying (stopping, finalizing, whatever) the threads after they are started and have run. My question then: "Is it necessary to stop/destroy these threads?" I was under the impression that they will fall out of scope and be automagically destroyed and collected. If this is wrong, I need to do some cleanup. Luckily if I am wrong, the services only create one thread each, and only on startup, so it's not going to kill the server, but I still would prefer to do it the correct way. Any thoughts or articles on this is greatly appreciated. Thanks!
Posted
I got that impression also - shouldn't GC take care of those after they are finished? If you want to be sure, test it... create 30 test threads which all takes some amount of memory, make them run for 1 minute and see if the memory usage drops after they are finished.
BS - Beer Specialist
  • Administrators
Posted

If the thread finishes it processing (i.e. exists it's main function) then it should automatically exit.

Also with the way GC works creating some threads and then waiting a minute wouldn't prove anything, resources may be awaiting garbage collection for much longer than a minute - it all depends on available resources.

 

Therer are tools you could use to monitor your service and check this - the built in task Manager, Performance monitor or Process Explorer

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

you can force the Garbage collector to clean up garbage with the Collect Method.

 

Are you using system.threading.thread for your threads? If so you can check the threads ThreadState to see if the thread is Threading.ThreadState.Stopped before starting the server. It helps if you name the threads with the name property. Then if the thread is running you can kill it with the abort method and call the garbage collector if needed.

 

Hope this helps.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...