Question about threads

Discussion in 'ASP .Net' started by michdoh@yahoo.com, Aug 14, 2006.

  1. Guest

    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

    ##################################################################
     
    , Aug 14, 2006
    #1
    1. Advertising

  2. sloan Guest

    "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.




    <> wrote in message
    news:...
    > 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
    >
    > ##################################################################
    >
     
    sloan, Aug 14, 2006
    #2
    1. Advertising

  3. 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)


    <> wrote in message
    news:...
    > 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
    >
    > ##################################################################
    >
     
    bruce barker \(sqlwork.com\), Aug 14, 2006
    #3
  4. Guest

    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



    bruce barker (sqlwork.com) wrote:
    > 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)
    >
    >
    > <> wrote in message
    > news:...
    > > 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
    > >
    > > ##################################################################
    > >
     
    , Aug 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Marden
    Replies:
    0
    Views:
    1,074
    Nick Marden
    Jan 17, 2004
  2. yoda
    Replies:
    2
    Views:
    464
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    433
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,485
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    176
    Une bévue
    Jun 14, 2006
Loading...

Share This Page