Results 1 to 17 of 17

Thread: [RESOLVED] Not enough free threads in the ThreadPool

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Resolved [RESOLVED] Not enough free threads in the ThreadPool

    Hello There,

    anybody familiar with these error? "There were not enough free threads in the ThreadPool object to complete the operation".

    im calling a web service in my windows application client...

    what is the possible solution to this problem?

    thanks
    glen

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Not enough free threads in the ThreadPool

    Have you tried increasing the maximum number of threads available to the ThreadPool? What is the current value? What exactly are you trying to do? Is there much else going on at the same time?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    thanks for a quick reply..

    i display a GIF image loading at the same time and calling a web service(do some processing here) in other thread..

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Not enough free threads in the ThreadPool

    So the chances are that you are either starting way tooo many threads, or they are not finishing correctly. How many threads do you think you should be using, and how long should they live for?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    Hi Grimfort,

    i only need two two thread to be running at the same time...1 is the UI thread for the GIF progressbar and 1 thread for the calling of my web service..

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    This are my code..

    Code:
    Private Delegate Sub DelUpdateProgressBar(ByVal Value As Boolean, ByVal errMsg As Exception, ByVal str As String)
    
    Private _DelUpdateProgressBar As New DelUpdateProgressBar(AddressOf UpdateSendingForm)
    
    Private Sub MultiThread()
    
          
            Dim bname As String = txtSite & sDB
            Dim msgRequest As String
            Dim itmDetail As String
    
            itmDetail = GetListView(ListViewItemSummary, " ")
    
            msgRequest = "Loan " & sDB & "/" & amt & "/" & empl & "/" & _
                          cus_id.Replace("-", "") & "/" & TextBox1.Text.Trim & "/" & itmDetail
    
            Try
    
                Dim mywebserv As testwebserver.TextIN = New testwebserver.TextIN
    
                If Not mywebserv.ValidateSenderInfo(bname) = Nothing Then
                    mywebserv.InsertIN(bname, msgRequest)
    
                    If Connect.State = ConnectionState.Closed Then
                        InitializeDB()
                    End If
    
                    Dim sqlcmd As SqlCommand = New SqlCommand
                    sqlcmd.Connection = Connect
    
                    sqlcmd.CommandText = "INSERT INTO TABLE1(col1,col2,col3,col4,col5)Values(@aa,@bb,@cc,@dd,@ee)"
    
                    sqlcmd.Parameters.Clear()
                    With sqlcmd.Parameters
                        .Add("@aa", SqlDbType.BigInt).Value = Convert.ToInt64(cus_id.Replace("-", ""))
                        .Add("@bb", SqlDbType.BigInt).Value = Convert.ToInt64(ptno.Replace("-", ""))
                        .Add("@cc", SqlDbType.Char).Value = res_id
                        .Add("@dd", SqlDbType.NVarChar).Value = TextBox1.Text.Trim
                        .Add("@ee", SqlDbType.Float).Value = Convert.ToDouble(amt)
                    End With
    
                    sqlcmd.ExecuteNonQuery()
                    HasLMTcode = True
    
                    UpdateSendingForm(False, Nothing, "")
                Else
                    UpdateSendingForm(False, Nothing, "Error")
                End If
    
            Catch ex As Exception
                UpdateSendingForm(False, ex, "")
            End Try
    
        End Sub
    
     Private Sub ButtonProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonProcess.Click
      
            Dim trd As New Thread(AddressOf MultiThread)
    
            Me.Button1.Enabled = False
            Me.PictureBox1.Visible = True
            Me.Label1.Visible = True
    
            trd.Start()
    
     End Sub
    
     Private Sub UpdateSendingForm(ByVal Value As Boolean, ByVal err As Exception, ByVal str As String)
          If Me.PictureBox1.InvokeRequired Then
                Me.PictureBox1.Invoke(New DelUpdateProgressBar(AddressOf UpdateSendingForm), New Object() {Value, err, str})
          Else
            Me.PictureBox1.Visible = Value
            Me.Label1.Visible = Value
            If str = "Error" Then
                MessageBox.Show("Error: HO Master File might not be updated.")
                Exit Sub
            End If
            If Not err Is Nothing Then
                MessageBox.Show(err.Message)
                Me.Button1.Enabled = True
                Exit Sub
            End If
            If Value = False Then
                MessageBox.Show("Request sent successfully.")
                Me.Button1.Enabled = True
                Me.Close()
            End If
          End If
        End Sub
    Last edited by [gja]; Mar 15th, 2011 at 09:12 PM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Not enough free threads in the ThreadPool

    Are you aware that you are accessing controls on the secondary thread in that code?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    Quote Originally Posted by jmcilhinney View Post
    Are you aware that you are accessing controls on the secondary thread in that code?
    yes im aware that's why im using delegates in accessing the control..

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Not enough free threads in the ThreadPool

    The delegates aren't doing anything useful though. Using a delegate doesn't inherently make accessing controls thread-safe. You have to call the Invoke method of a control in order to cross the thread boundary and invoke the delegate on the UI thread, which you're not doing. You're invoking your delegates on the secondary thread so they are completely useless. Follow the CodeBank link in my signature and check out my post on Accessing Threads From Worker Threads to find out how to do it properly.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    Quote Originally Posted by jmcilhinney View Post
    The delegates aren't doing anything useful though. Using a delegate doesn't inherently make accessing controls thread-safe. You have to call the Invoke method of a control in order to cross the thread boundary and invoke the delegate on the UI thread, which you're not doing. You're invoking your delegates on the secondary thread so they are completely useless. Follow the CodeBank link in my signature and check out my post on Accessing Threads From Worker Threads to find out how to do it properly.
    thank you very much jmc..i will check that right now and post back if i have problem..

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    i have updated my code at post #6 jmc please try to see if it is completely got it...

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Not enough free threads in the ThreadPool

    That looks much better, although you're still accessing TextBox1 a couple of times in your 'MultiThread'. Are you still getting the same error message?

    On a style note, "MultiThread" is a bad name for that method. That method has nothing to do with multi-threading. You should name the method for its purpose. The method itself doesn't care what thread it's executed on.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    Quote Originally Posted by jmcilhinney View Post
    That looks much better, although you're still accessing TextBox1 a couple of times in your 'MultiThread'. Are you still getting the same error message?

    On a style note, "MultiThread" is a bad name for that method. That method has nothing to do with multi-threading. You should name the method for its purpose. The method itself doesn't care what thread it's executed on.
    so far, for my testing i cannot reproduce the error...is that the real cause of my problem?

    changed the method name also..:-)

    anyway,Thank you very much for help...threading is really a very challenging topic...

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Not enough free threads in the ThreadPool

    jmc, the system prevent me to rate your post...i remember i give you reputation in the past...does it mean that i cannot give you reputation again?...

    i mark this thread as resolved...thank you jmc

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Not enough free threads in the ThreadPool

    Accessing controls on the wrong thread can produce unexpected behaviour. That's why I honed in on that to begin with, i.e. I thought that that might have been the cause of your issue. If the issue has gone away then I'm guessing that it was.

    You can only rate the same person in cycles of 10. Don't sweat it. Your undying admiration is quite enough reward.

  16. #16
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [RESOLVED] Not enough free threads in the ThreadPool

    Bah, don't make his head any bigger!

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] Not enough free threads in the ThreadPool

    Quote Originally Posted by Grimfort View Post
    Bah, don't make his head any bigger!
    Why not? I often feel like one of the creatures in locker C18, with Agent "K" dropping by, imparting knowledge.


    Grand Central Station Locker Creatures: K is back! The keeper of the light! All hail K! All hail K! Oh K can you see by the dawn's early light...


    Cost of Visual Studio Ultimate: $11,899

    Cost of VBFourms Membership: $0

    Having jmcilhinney rate one of your posts: Priceless

    Someone should pitch this to Mastercard...
    Last edited by dbasnett; Mar 16th, 2011 at 08:07 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width