adding asynchronous support

  • Thread starter Thread starter matt
  • Start date Start date
M

matt

i have a VB.net application that connects to the internet to download an xml
file and parse it. It works correctly, but "locks up" until that function
returns. i want to add asynchronous support, but i'm confused on where i
add the code. If i want just this function to be in another thread, what do
i add to it? Also, what do i add to call the function? thanks,
 
Something like...

IMPORTS System.Threading

Private Sub frmMe_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objThread As New Thread(AddressOf fnThreadToRun)
objThread.IsBackground = True
objThread.Priority = ThreadPriority.Lowest
objThread.Name = "MyThread"
objThread.Start()
end sub

Private Sub fnThreadToRun()
'.... do something
End Sub

Regards,

P
 
hey this is great. It was exactly what i needed. I played around w/ adding
a second thread to my program. I learned that is has to be a subroutine and
that you can't "pass parameters" you have to pull them from the thread,
which is fine, and i made a flag so i can check when its done, however, i'm
having trouble getting the parameters it saved. i have variables and a sub
in a class. I have an object in my program for hte class, and then another
object that is the subroutine from the class. the subroutine stores
variables within that class. i used to pull the variables, they were not
returns, however, now i can't get to these. here is the basic psuedo code

class1:
var1
var2
sub1()
do something with var1
store in var2

myobject = class1
thread = myobject.sub1

myobject.var1 = something
thread.start()
do something with myobject.var2

well, var1 and var2 are empty outside of the thread, but populated inside
the thread. i used this same approach w/o the thread, and i was able to see
my varibles. Is there some reason my thread isn't storing variables like
the sub did without the thread?

thanks for your help!
 
disregard that! i complete forgot that it was in another thread. it was
trying to use the parameters before it was done processing them. it works
perfectly! thanks
 
Hey thanks again for getting me started on threads. I have another
question. I initiated the thread at the top of my code, and changed the
settings in the "load" sub. Later in my program, i started the thread, and
it ran fine. Where i'm having trouble is when it stops and i want to run it
again. How should i rerun the thread? When my code detects that the thread
has completed, i tried to use thread.abort, but when i do thread.start
again, i get an error saying the thread has stopped and can't be restarted.
How do i restart it? Do I reinitiate the thread each time? I tried that by
initiating the thread once at the begining, and again each time i want it to
run again. There is one problem, when i look at my program in Task Manager,
each time it runs the thread, it adds a new thread. Do i have to kill the
thread each time? Anyway, did i miss the concept of how this works?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top