Subs in separate threads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Subs in separate threads

    Hi,

    I have an application that needs to run subs simultaneously - possibly 2 or 3 instances at a time - which might take a few seconds to process. The Sub requires variables so it cannot be a straightforward thread request.

    I'm not sure a backgroundWorke r is the answer since I think it needs to finish running before it can be run again?

    I found some code that uses threads but it completely crashes the entire application which shuts itself down. The application imports System.Threadin g etc.

    The code used to run the sub is as follows.

    Code:
    Dim mySub As New Thread(DirectCast(Sub() processVal(thisval), ThreadStart))
    mySub.Start()
    The sub processVal runs some calcs and requests data from a 3rd party site.

    Are there some extra requirements, imports etc that I should be aware of when using this type of threading procedure? Or a better way to have simultaneous subs running?

    Thanks

    EDIT: Is there maybe some kind of Async method to do this?
  • robertybob
    New Member
    • Feb 2013
    • 116

    #2
    Managed to stop the crash by adding the line

    Code:
    mySub.SetApartmentState(ApartmentState.STA)
    however now have cross-threading issues so will have a look at that.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      ok - so the crash and cross-threading is ok but would still be good to know if there's a simple Async solution

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Will this MSDN article help you: Calling Synchronous Methods Asynchronously?

        Comment

        Working...