By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

Subs in separate threads

100+
P: 116
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 backgroundWorker 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.Threading etc.

The code used to run the sub is as follows.

Expand|Select|Wrap|Line Numbers
  1. Dim mySub As New Thread(DirectCast(Sub() processVal(thisval), ThreadStart))
  2. mySub.Start()
  3.  
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?
Nov 6 '14 #1

✓ answered by robertybob

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

Share this Question
Share on Google+
3 Replies


100+
P: 116
Managed to stop the crash by adding the line

Expand|Select|Wrap|Line Numbers
  1. mySub.SetApartmentState(ApartmentState.STA)
  2.  
however now have cross-threading issues so will have a look at that.
Nov 7 '14 #2

100+
P: 116
ok - so the crash and cross-threading is ok but would still be good to know if there's a simple Async solution
Nov 7 '14 #3

Frinavale
Expert Mod 5K+
P: 9,731
Will this MSDN article help you: Calling Synchronous Methods Asynchronously?
Nov 7 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.