Passing Parameter To Thread Starter

Posted on 2006-05-23
Last Modified: 2010-04-23

I have a program that needs to watch mulitple URL's to make sure they are online.

I want to create a new thread for each URL.

The problem I am having is being able to let the thread know which URL it should be checking as I can't seem to be able to pass a parameter to the thread starter.

I impliment a loop which will grab a URL and pass it to SpinUp which in turn creates the thread and initiates the module called startme.

StartMe does the actual work of checking the availability of the URL. However I need to pass the URL into StartMe so it knows which URL to check.

I have tried setting a private variable from SpinUp and let StartMe read that variable. However the thread in StartMe doesn't begin after each call to SpinUp. It seems that SpinUp starts multiple threads befor StartMe can begin.

I have tried to create StartMe with arguments however the ThreadStart method in SpinUp doesn't like that.

Any ideas on how I can do this?

Public Sub SpinUp(ByVal URL As String)
        'Purpose: Create threads
        ' each of which will watch one url
            Dim start As New ThreadStart(AddressOf Me.StartMe)
            _thread = New Thread(start)
        Catch ex As Exception

        End Try

    End Sub

Public Sub StartMe()
          ' Check the staus of the URL
       End Try
End Sub
Question by:Sling_Blade
    LVL 5

    Accepted Solution

    this is a classic question for .net

    Thead.Start doesnt take any type of params.

    I solve this problem by wrapping my threads w/ a class.  You can then abstract the start method w/ your own, passing a value that you will make global inside of your class.  What this does is abstracts the thread handling for you so you can create a controller class that will handle these guys.  If you want even more complexity, add custom events for shutting down your threads cleanly and catching failure events such as the URL is down.

    for example

    Public Class ThreadWrapper
         Private innerThread as Thread
         Private url as String

         Public Sub New(byval url as String)
              Me.url = url
              Me.innerThread = new Thread(new ThreadStart(addressof Me.StartCheckingUrl))
         End Sub

         Public Sub StartCheckingUrl()
         End Sub

         Private Sub CheckURL()
              ' Check the staus of Me.Url With out problems...
              End Try
         End Sub
    End Class

    Author Comment

    Thanks for the example class. That really helped cement it for me.
    LVL 5

    Expert Comment

    roger that

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now