Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Managing Multiple Threads

Status
Not open for further replies.

Meleagant

Programmer
Aug 31, 2001
166
0
0
US
All,

I've been looking all day and I haven't been able to find an answer (at least one that I can understand).

The problem is simply that in a business process I have to execute the same Exe say 12 times with different parameters. The number of times will vary depending on circumstances.

Instead of running these 12 Exes sequentially, I would like to run them asynchronously. Say 3 at a time.

I'm used to spinning new threads, what I can't seem to understand is how do I control the number of running threads and when one completes how to loop around and execute the next executable with a new set of arguments.

Can someone point me in the right direction or give me an example on how to do this?

* Sine scientia ars nihil est
* Respondeat superior
 
Here is my first rudimentary attempt at accomplishing my goal. There are a number of executables that currently run synchronously in a job stream. I would like to run say 2 or 3 at a time until all of the exes have been run. This is just a rough draft and wondering if anyone has any helpful comments.

I took an example from this article at Tech Republic

Create a new Console Application in VS and paste this code in:
Code:
Imports System.Threading

Module Module1

    Sub Main()

        SyncLockMultiThreadComputation(10, 0)

    End Sub

    Public Class ThreadTestUtilities
        Public Function Compute(ByVal InputValue As Double) As Double
            Dim DoubleOutputValue As Double
            Dim DateTimeNow As DateTime
            Dim rndNumberGenerator As System.Random
            DateTimeNow = New DateTime(DateTime.Now.Ticks)
            rndNumberGenerator = New System.Random(DateTimeNow.Hour + DateTimeNow.Minute + DateTimeNow.Millisecond)
            If DateTimeNow.Millisecond > 500 Then
                DoubleOutputValue = System.Math.IEEERemainder(System.Math.Exp(rndNumberGenerator.Next * (InputValue + 5000) * System.Math.E), rndNumberGenerator.Next)
            Else
                DoubleOutputValue = rndNumberGenerator.Next(InputValue) / System.Math.Max(Double.MaxValue - 1, System.Math.Log(System.Math.Pow(System.Math.PI, InputValue)))
            End If
            DateTimeNow = Nothing
            rndNumberGenerator = Nothing
            Return DoubleOutputValue
        End Function

        Public Sub SingleThreadComputation(ByVal Iterations As Integer)
            Dim IntegerIterationCounter As Integer
            For IntegerIterationCounter = 1 To Iterations
                Compute(Double.Parse(IntegerIterationCounter))
            Next
        End Sub
    End Class

    Public Class SyncLockThreadWorker
        Public Shared objStorageLock As New Object
        Public Shared objCompletedComputationsLock As New Object
        Public Shared objRunningThreadsLock As New Object
        Public Shared IntegerCompletedComputations As Integer = 0
        Public Shared iRunningThreads As Integer = 0
        Private Shared DoubleStorage As Double

        Public Property Storage() As Double
            Get
                SyncLock objStorageLock
                    Return DoubleStorage
                End SyncLock
            End Get
            Set(ByVal value As Double)
                SyncLock objStorageLock
                    DoubleStorage = value
                End SyncLock
            End Set
        End Property

        Public Property CompletedComputations() As Integer
            Get
                Return IntegerCompletedComputations
            End Get
            Set(ByVal value As Integer)
                IntegerCompletedComputations = value
            End Set
        End Property

        Public Shared Property RunningThreads() As Integer
            Get
                Return iRunningThreads
            End Get
            Set(ByVal value As Integer)
                iRunningThreads = value
            End Set
        End Property

        Public Sub ThreadProc(ByVal StateObject As Object)
            Dim ttuComputation As ThreadTestUtilities
            ttuComputation = New ThreadTestUtilities
            Storage = ttuComputation.Compute(CDbl(StateObject))

            SyncLock objCompletedComputationsLock
                Thread.Sleep(3000)
                CompletedComputations += 1
            End SyncLock

            SyncLock objRunningThreadsLock
                RunningThreads -= 1
            End SyncLock

            ttuComputation = Nothing
        End Sub

        Public Shared Sub IncrementRunningThread()
            SyncLock objRunningThreadsLock
                RunningThreads += 1
            End SyncLock
        End Sub

        Public Sub New()

        End Sub
    End Class

    Public Sub SyncLockMultiThreadComputation(ByVal Iterations As Integer, Optional ByVal ThreadCount As Integer = 0)
        Dim twSyncLock As SyncLockThreadWorker
        Dim IntegerIterationCounter As Integer
        Dim iOriginalMaxThreads As Integer
        Dim iOriginalMinThreads As Integer
        Dim iOriginalMaxIOThreads As Integer
        Dim iOriginalMinIOThreads As Integer

        Dim maxNumberOfRunningThreads As Integer = 2

        twSyncLock = New SyncLockThreadWorker
        Threading.ThreadPool.GetMaxThreads(iOriginalMaxThreads, iOriginalMaxIOThreads)
        Threading.ThreadPool.GetMinThreads(iOriginalMinThreads, iOriginalMinIOThreads)
        If ThreadCount > 0 Then
            Threading.ThreadPool.SetMaxThreads(ThreadCount, ThreadCount)
            Threading.ThreadPool.SetMinThreads(ThreadCount, ThreadCount)
        End If

        SyncLockThreadWorker.iRunningThreads = 0

        While SyncLockThreadWorker.IntegerCompletedComputations < Iterations
            'For IntegerIterationCounter = 1 To Iterations
            While SyncLockThreadWorker.iRunningThreads < maxNumberOfRunningThreads
                SyncLockThreadWorker.IncrementRunningThread()
                Threading.ThreadPool.QueueUserWorkItem(AddressOf twSyncLock.ThreadProc, Double.Parse(IntegerIterationCounter))
            End While
            'Next
        End While

        While SyncLockThreadWorker.IntegerCompletedComputations < Iterations AndAlso SyncLockThreadWorker.iRunningThreads > 0

        End While

        Threading.ThreadPool.SetMaxThreads(iOriginalMaxThreads, iOriginalMaxIOThreads)
        Threading.ThreadPool.SetMinThreads(iOriginalMinThreads, iOriginalMinIOThreads)

        twSyncLock = Nothing
        IntegerIterationCounter = Nothing
    End Sub


End Module

* Sine scientia ars nihil est
* Respondeat superior
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top