Passing Parameter To Thread Starter

Posted on 2006-05-23
Medium Priority
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
  • 2

Accepted Solution

mydasx earned 2000 total points
ID: 16747317
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

ID: 16747451
Thanks for the example class. That really helped cement it for me.

Expert Comment

ID: 16753129
roger that

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

569 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