Multithreading question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fniles

    #1

    Multithreading question

    I am using VB.NET 2005 and thread.
    In each thread I open a database connection and before it exits the thread I
    close the database connection.
    Is there any way for me to count how many threads are still working so that
    if I know there is say 50 threads are working, I want to hold on and do not
    create any more thread until the previous threads are finished ?
    Thank you


  • eBob.com

    #2
    Re: Multithreading question

    I'm not one of the heavy hitters here, but what I do in a similar situation
    is to use a simple counter. In my case the incrementing doesn't need to be
    synchronized. To decrement the counter I use ...

    SitesToGo = Interlocked.Dec rement(globalin f.TotalSites)

    If you need to synchronize the incrementing I am sure there is an
    Interlock.Incre ment method.

    When you use counters like this you have to be very sure they get
    incremented and decremented in all the right places. It's easy to miss
    error handling paths.

    Bob

    "fniles" <fniles@pfmail. comwrote in message
    news:eg84rM4uIH A.5580@TK2MSFTN GP04.phx.gbl...
    >I am using VB.NET 2005 and thread.
    In each thread I open a database connection and before it exits the thread
    I close the database connection.
    Is there any way for me to count how many threads are still working so
    that if I know there is say 50 threads are working, I want to hold on and
    do not create any more thread until the previous threads are finished ?
    Thank you
    >

    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Multithreading question

      If you use a threadpool you can,

      http://msdn.microsoft.com/en-us/libr...ol(VS.71).aspx

      However why are you using a thread to open a connection, as that is the
      first step in some real synchronous actions.

      (Not meant for you, you are probably paid for the time your programs consume
      processor time, however for others who get this idea)

      Cor

      "fniles" <fniles@pfmail. comschreef in bericht
      news:eg84rM4uIH A.5580@TK2MSFTN GP04.phx.gbl...
      >I am using VB.NET 2005 and thread.
      In each thread I open a database connection and before it exits the thread
      I close the database connection.
      Is there any way for me to count how many threads are still working so
      that if I know there is say 50 threads are working, I want to hold on and
      do not create any more thread until the previous threads are finished ?
      Thank you
      >

      Comment

      • Just_a_fan@home.net

        #4
        Re: Multithreading question

        Each thread knows it's number. You could log them in some way. Maybe
        have each one set it's element in an array to true coming in and false
        going out and you could run the array and count bits before starting up
        another one. Fairly cheap on processor time, I expect.

        I have been wondering about how to serialize also. No answer seen yet.
        Still hoping...

        Mike

        On Wed, 21 May 2008 15:42:42 -0500, in
        microsoft.publi c.dotnet.langua ges.vb "fniles" <fniles@pfmail. comwrote:
        >I am using VB.NET 2005 and thread.
        >In each thread I open a database connection and before it exits the thread I
        >close the database connection.
        >Is there any way for me to count how many threads are still working so that
        >if I know there is say 50 threads are working, I want to hold on and do not
        >create any more thread until the previous threads are finished ?
        >Thank you
        >

        Comment

        • cj

          #5
          Re: Multithreading question

          I have a class:

          Public Class MyThreadCount

          Private Shared m_lock As New Object
          Private Shared m_threadcount As Int32 = 0

          Public Shared Sub Increment()
          SyncLock (m_lock)
          m_threadcount += 1
          End SyncLock
          End Sub

          Public Shared Sub Decrement()
          SyncLock (m_lock)
          m_threadcount -= 1
          End SyncLock
          End Sub

          Public Shared ReadOnly Property ThreadCount() As Int32
          Get
          Dim _count As Int32
          SyncLock (m_lock)
          _count = m_threadcount
          End SyncLock
          Return _count
          End Get
          End Property
          End Class

          When a thread starts it increments the counter:

          MyThreadCount.I ncrement()

          When it ends it decrements the counter:

          MyThreadCount.D ecrement()

          The main program has a timer that looks at MyThreadCount.T hreadCount and
          displays how many threads are currently running as an indicator of how
          much work the program is doing. Also when the program goes to shutdown
          I stop creating new threads and wait for the threadcount to come down to
          0 before stopping.

          This seems like what Bob suggested but I'm not familiar with Interlock.
          Perhaps it does what synclock does and prevents multiple threads from
          stepping on each other when incrementing and decrementing the counter.


          fniles wrote:
          I am using VB.NET 2005 and thread.
          In each thread I open a database connection and before it exits the thread I
          close the database connection.
          Is there any way for me to count how many threads are still working so that
          if I know there is say 50 threads are working, I want to hold on and do not
          create any more thread until the previous threads are finished ?
          Thank you
          >
          >

          Comment

          • fniles

            #6
            Re: Multithreading question

            Thank you.
            >However why are you using a thread to open a connection, as that is the
            >first step in some real synchronous actions.
            In the thread I need to read a few queries from the database, if I open the
            connection in the main program and sends the connection to the thread, won't
            I run into the error "you can only have 1 datareader per connection", or
            something like that ?



            "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
            news:D8746C50-A125-4665-9810-4FABECE27D28@mi crosoft.com...
            If you use a threadpool you can,
            >
            http://msdn.microsoft.com/en-us/libr...ol(VS.71).aspx
            >
            However why are you using a thread to open a connection, as that is the
            first step in some real synchronous actions.
            >
            (Not meant for you, you are probably paid for the time your programs
            consume processor time, however for others who get this idea)
            >
            Cor
            >
            "fniles" <fniles@pfmail. comschreef in bericht
            news:eg84rM4uIH A.5580@TK2MSFTN GP04.phx.gbl...
            >>I am using VB.NET 2005 and thread.
            >In each thread I open a database connection and before it exits the
            >thread I close the database connection.
            >Is there any way for me to count how many threads are still working so
            >that if I know there is say 50 threads are working, I want to hold on and
            >do not create any more thread until the previous threads are finished ?
            >Thank you
            >>
            >

            Comment

            • fniles

              #7
              Re: Multithreading question

              Thank you.

              Can I do the following:
              1. check the number of threads
              2. if the number of threads >= 100 then loop until the number of threads <
              100
              like so:

              Dim EachMessageThre ad As Threading.Threa d = Nothing

              EachMessageThre ad = New Threading.Threa d(AddressOf
              clsEachMessage. ProcessMessage)
              do while myThreadCount.T hreadCount >= 100
              thread.sleep(10 0)
              loop
              MyThreadCount.I ncrement()
              EachMessageThre ad.Start()



              "cj" <cj@nospam.nosp amwrote in message
              news:Osle2sNvIH A.3680@TK2MSFTN GP05.phx.gbl...
              >I have a class:
              >
              Public Class MyThreadCount
              >
              Private Shared m_lock As New Object
              Private Shared m_threadcount As Int32 = 0
              >
              Public Shared Sub Increment()
              SyncLock (m_lock)
              m_threadcount += 1
              End SyncLock
              End Sub
              >
              Public Shared Sub Decrement()
              SyncLock (m_lock)
              m_threadcount -= 1
              End SyncLock
              End Sub
              >
              Public Shared ReadOnly Property ThreadCount() As Int32
              Get
              Dim _count As Int32
              SyncLock (m_lock)
              _count = m_threadcount
              End SyncLock
              Return _count
              End Get
              End Property
              End Class
              >
              When a thread starts it increments the counter:
              >
              MyThreadCount.I ncrement()
              >
              When it ends it decrements the counter:
              >
              MyThreadCount.D ecrement()
              >
              The main program has a timer that looks at MyThreadCount.T hreadCount and
              displays how many threads are currently running as an indicator of how
              much work the program is doing. Also when the program goes to shutdown I
              stop creating new threads and wait for the threadcount to come down to 0
              before stopping.
              >
              This seems like what Bob suggested but I'm not familiar with Interlock.
              Perhaps it does what synclock does and prevents multiple threads from
              stepping on each other when incrementing and decrementing the counter.
              >
              >
              fniles wrote:
              >I am using VB.NET 2005 and thread.
              >In each thread I open a database connection and before it exits the
              >thread I close the database connection.
              >Is there any way for me to count how many threads are still working so
              >that if I know there is say 50 threads are working, I want to hold on and
              >do not create any more thread until the previous threads are finished ?
              >Thank you

              Comment

              Working...