Track number of background threads running with vb.net

Good morning all. Last night I said goodbye to a bunch of people I have been working with for the last 6 weeks. They really were a good group of people. They came from all walks of life and had different situations to be sure but we also had a lot in common. It was very time consuming but I think I am going to miss seeing them. I certainly got so much out of it. I will be seeing a smaller group of them only once a week from now on for a while. So we will see how that goes.

I needed to track the number of background threads running for a project. Eventually this will turn into being able to address the threads programmatically by index but this is all you need to make this code work. Hope you are all well and make it a great day!


Join me on Facebook

Create an Object and Counter that all Threads can access:

    Public Class MySync
        Public Shared SyncObject As New Object
        Public Shared ThreadCount As Integer
    End Class

Then use SyncLock like this:

    Public Shared Function ScanDB()
        ‘ thread is starting: increase count
        SyncLock MySync.SyncObject
            MySync.ThreadCount = MySync.ThreadCount + 1
            ‘ …possible other code in here…
        End Synclock
        ‘ …do stuff in here…
        ‘ thread is done and will exit below: decrease count
        SyncLock MySync.SyncObject
            MySync.ThreadCount = MySync.ThreadCount – 1
            ‘ …possible other code in here…
        End Synclock
    End Function

Consulting Requests

Feedback

Blog Front Page

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: