Question about threads


M

michdoh

Hi All

I'm looking for some help on creating a basic multi threaded
application.
i'n new to threads in this environment so for test purposes I've
produced a very basic program. (which doesn't work)

Calling the 2 methods directly has the desired effect however when I
try to impelment them as threads nothing happens.

Any help os greatfully recieved.

Mikey
################################################
Private slTimer As New Thread(AddressOf SlowOut)
Private ftTimer As New Thread(AddressOf FastOut)

Private Sub btStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btStart.Click
slTimer.Start()
ftTimer.Start()
End Sub

Sub FastOut()
Dim x As Integer = 0
While x < 5
Response.BufferOutput = False
Response.Write("Fast " & x & " " &
AppDomain.GetCurrentThreadId & "<BR>")
Response.Flush()
x += 1
ftTimer.Sleep(200)
End While
End Sub


Sub SlowOut()
Dim i As Integer = 0
While i < 5
Response.BufferOutput = False
Response.Write("Slow" & i & " " &
AppDomain.GetCurrentThreadId & "<BR>")
Response.Flush()
slTimer.Sleep(500)
i += 1
End While
End Sub

##################################################################
 
Ad

Advertisements

S

sloan

"desired effect?"

are you meaning you want to make sure 1 thread finishes before the other
thread?
that's not a good idea. you shoujd not design your app to rely on one
thread finishing before another.

however, you can design an app to say "whichever thread finishes first, it
needs to wait on the other thread(s)".


You need to search for things like

"Thread.Join"

"thread racing"

and if you're 2 threads might want access to the same object, you need to
figure out how to lock an item.
 
B

bruce barker \(sqlwork.com\)

web applications are request based. when the browser requests a page,
asp.net constructs a class instance of the requested page, then calls the
processing methods to build the response (render) then closes the connection
to the browser.

if you start a thread, it can not send any data after render back to the
browser. you need to have the main thread stall, (say in onload) until the
other threads are finished.

in your case you are not starting new threads, but using a timer, which
calls back to the current thread. in this case you need the main thread to
sleep after starting the timers. it may have to loop and go back to sleep
after each callback.

note: there is a page timeout you may need to override. also you need to
turn off buffering or the browser will not display anything until the page
sleep exits.

-- bruce (sqlwork.com)
 
Ad

Advertisements

M

michdoh

Hi Guys

The actual process will be as follows.

I will be consuming a web service that returns a value to the 1st
thread.
If the service does not return the result withing 1 min I wish to
resend the request.
I would wish to resend the request in 1 - 2 -3 min intervals until
finally killing the process.

A appreciate there are other ways to achieve this however I though
runing one thread to make the request, One to set the timer and
possibly a third as a coordinate module might be a fun way to achieve
this.

As a proof of concept I have writen this very basic program to get 2
threads to run independently while outputting to he browser. Both
threads have no interaction apart form using the output buffer.

The code above does not output until the thread has died however If I
up the output string to over 254 Char I can get this to output one line
at a time. (I believe this is a problem with IE)

If I call each method they run and sleep as required however
(obviuosly) the 2nd does not start until the first is finished.

What I do not understand is, when if I start the threads (which run the
methods independently) I get no output.

Any help would be great

Mikey
 

Top