Start 20 Threads, stop them all after 10 seconds

Discussion in 'ASP .Net' started by Mark B, Jun 18, 2005.

  1. Mark B

    Mark B Guest

    Hi

    I was wondering if someone could help me with the syntax for some VB.Net
    ASPX code to:

    - Start 20 threads
    - End all threads after the sooner of 10 seconds or if all of the threads
    themselves have finished.

    BTW, each thread checks an email address for validity with www.aspnetmx.com.
    If it is taking too long (e.g. > 10 seconds) I just want to look at the
    results for the threads that have finished.

    TIA
    Mark
     
    Mark B, Jun 18, 2005
    #1
    1. Advertising

  2. Mark B

    Cor Ligthert Guest

    Mark,

    The most simple way is to set them first in an array and than to abort them
    using a timer in your main program. (I doubt if it really will work.)

    As I understand it well, does this mean that if you have 100 sessions
    active, that than you have 2000 threads active in your system.

    Now I see even less purpose of your threading. (Probably your processor time
    is eaten by the processing of those threads)

    However just my thought of course.

    Cor
     
    Cor Ligthert, Jun 18, 2005
    #2
    1. Advertising

  3. Mark B

    m.posseth Guest

    well in my opinion a asynchrone started dellegate would be a much cleaner
    solution in this situation
    just my 2 cents

    Michel Posseth [MCP]


    "Mark B" <> schreef in bericht
    news:...
    > Hi
    >
    > I was wondering if someone could help me with the syntax for some VB.Net
    > ASPX code to:
    >
    > - Start 20 threads
    > - End all threads after the sooner of 10 seconds or if all of the threads
    > themselves have finished.
    >
    > BTW, each thread checks an email address for validity with
    > www.aspnetmx.com.
    > If it is taking too long (e.g. > 10 seconds) I just want to look at the
    > results for the threads that have finished.
    >
    > TIA
    > Mark
    >
    >
    >
    >
    >
     
    m.posseth, Jun 18, 2005
    #3
  4. Mark B

    Mark B Guest

    Thanks. OK then, here's the code I could use:

    '********CODE START***************
    Dim intResult(20) as Integer = 1
    Dim i as Integer
    'intResult(i) 1 = Not Finished, 2 = True , 3 =False

    For i = 1 To 20

    '*******Start thread (i) here
    If Mx.Validate(Session("Email"+Format$(i,"00")))=True Then
    'This tests the particular email address
    intResult(i)= 2
    Else
    intResult(i)= 3
    End If
    ' *********End thread (i)i here

    Next i

    '********CODE END***************


    Can someone help me with the threading syntax I should use above where it
    says:

    '*******Start thread i here
    and
    ' *********End thread i here

    and also the main engine code to shut down the threads after 10 seconds.
    Then the 20 session variables can be inspected to see which ones passed,
    failed and timed-out.


    TIA
    Mark

    P.S. "As I understand it well, does this mean that if you have 100 sessions
    active, that than you have 2000 threads active in your system." True, but
    the threads are only active for 10 seconds per session. We can see how it
    goes.




    "Cor Ligthert" <> wrote in message
    news:...
    > Mark,
    >
    > The most simple way is to set them first in an array and than to abort

    them
    > using a timer in your main program. (I doubt if it really will work.)
    >
    > As I understand it well, does this mean that if you have 100 sessions
    > active, that than you have 2000 threads active in your system.
    >
    > Now I see even less purpose of your threading. (Probably your processor

    time
    > is eaten by the processing of those threads)
    >
    > However just my thought of course.
    >
    > Cor
    >
    >
     
    Mark B, Jun 20, 2005
    #4
  5. Mark,
    In addition to the other comments:

    Directly creating threads can be costly, rather then use New Thread, I would
    recommend using either the ThreadPool, creating your own thread pool, or
    using an async delegate.

    The following MSDN Magazine article shows one method of creating a timed
    method.

    http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/default.aspx


    Depending on how you call www.aspnetmx.com, for example if its a Web Service
    wouldn't it be easier to simply set the WebClientProtocol.Timeout property
    to your timeout amount (10 seconds)?

    http://msdn.microsoft.com/library/d...otocolswebclientprotocolclasstimeouttopic.asp

    Something like:

    '********CODE START***************

    ' set the method timeout on the proxy to 10 seconds
    Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)


    Dim intResult(20) as Integer = 1
    Dim i as Integer
    'intResult(i) 1 = Not Finished, 2 = True , 3 =False

    For i = 1 To 20

    '*******Start thread (i) here
    If Mx.Validate(Session("Email"+Format$(i,"00")))=True Then
    'This tests the particular email address
    intResult(i)= 2
    Else
    intResult(i)= 3
    End If
    ' *********End thread (i)i here

    Next i

    '********CODE END***************

    Also, depending on how may total requests I had coming in, I would consider
    doing these 20 outgoing requests asynchronously.

    Something like (syntax checked only):

    Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)

    Dim intResults(20 - 1) As Integer

    Dim results(20 - 1) As IAsyncResult

    ' Queue up all the requests
    For index As Integer = 0 To 20 - 1

    intResults(index) = 1
    results(index) =
    Mx.BeginValidate(Session("Email"+Format$(i,"00)), nothing, nothing)

    Next index

    ' wait for each request to finish
    For index As Integer = 0 To 20 - 1
    If Mx.EndValidate(results(index)) Then
    'This tests the particular email address
    intResults(index)= 2
    Else
    intResults(index)= 3
    End If
    Next

    NOTE: You may need the Mx.EndValidate in a Try/Catch as the Timeout will
    cause an exception to be raised...

    The advantage of using BeginValidate is that all 20 validates are executing
    concurrently...

    Hope this helps
    Jay

    "Mark B" <> wrote in message
    news:...
    | Hi
    |
    | I was wondering if someone could help me with the syntax for some VB.Net
    | ASPX code to:
    |
    | - Start 20 threads
    | - End all threads after the sooner of 10 seconds or if all of the threads
    | themselves have finished.
    |
    | BTW, each thread checks an email address for validity with
    www.aspnetmx.com.
    | If it is taking too long (e.g. > 10 seconds) I just want to look at the
    | results for the threads that have finished.
    |
    | TIA
    | Mark
    |
    |
    |
    |
    |
     
    Jay B. Harlow [MVP - Outlook], Jun 20, 2005
    #5
  6. Mark B

    Mark B Guest

    Thanks very much.

    I haven't really programmed much in VB.Net before just VBA.

    I couldn't find the method "BeginValidate" or "EndValidate" in the ASPNetMX
    documentation at http://www.aspnetmx.com/help/default.htm. Where does it
    come from?

    Also, excuse my .Net ignorance but does "asynchronously" mean at the same
    time?

    And, is "For index As Integer = 0 To 20 - 1" the same as:

    Dim index as Integer
    For index = 0 To 19



    "Jay B. Harlow [MVP - Outlook]" <> wrote in message
    news:...
    > Mark,
    > In addition to the other comments:
    >
    > Directly creating threads can be costly, rather then use New Thread, I

    would
    > recommend using either the ThreadPool, creating your own thread pool, or
    > using an async delegate.
    >
    > The following MSDN Magazine article shows one method of creating a timed
    > method.
    >
    > http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/default.aspx
    >
    >
    > Depending on how you call www.aspnetmx.com, for example if its a Web

    Service
    > wouldn't it be easier to simply set the WebClientProtocol.Timeout property
    > to your timeout amount (10 seconds)?
    >
    >

    http://msdn.microsoft.com/library/d...otocolswebclientprotocolclasstimeouttopic.asp
    >
    > Something like:
    >
    > '********CODE START***************
    >
    > ' set the method timeout on the proxy to 10 seconds
    > Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)
    >
    >
    > Dim intResult(20) as Integer = 1
    > Dim i as Integer
    > 'intResult(i) 1 = Not Finished, 2 = True , 3 =False
    >
    > For i = 1 To 20
    >
    > '*******Start thread (i) here
    > If Mx.Validate(Session("Email"+Format$(i,"00")))=True Then
    > 'This tests the particular email address
    > intResult(i)= 2
    > Else
    > intResult(i)= 3
    > End If
    > ' *********End thread (i)i here
    >
    > Next i
    >
    > '********CODE END***************
    >
    > Also, depending on how may total requests I had coming in, I would

    consider
    > doing these 20 outgoing requests asynchronously.
    >
    > Something like (syntax checked only):
    >
    > Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)
    >
    > Dim intResults(20 - 1) As Integer
    >
    > Dim results(20 - 1) As IAsyncResult
    >
    > ' Queue up all the requests
    > For index As Integer = 0 To 20 - 1
    >
    > intResults(index) = 1
    > results(index) =
    > Mx.BeginValidate(Session("Email"+Format$(i,"00)), nothing, nothing)
    >
    > Next index
    >
    > ' wait for each request to finish
    > For index As Integer = 0 To 20 - 1
    > If Mx.EndValidate(results(index)) Then
    > 'This tests the particular email address
    > intResults(index)= 2
    > Else
    > intResults(index)= 3
    > End If
    > Next
    >
    > NOTE: You may need the Mx.EndValidate in a Try/Catch as the Timeout will
    > cause an exception to be raised...
    >
    > The advantage of using BeginValidate is that all 20 validates are

    executing
    > concurrently...
    >
    > Hope this helps
    > Jay
    >
    > "Mark B" <> wrote in message
    > news:...
    > | Hi
    > |
    > | I was wondering if someone could help me with the syntax for some VB.Net
    > | ASPX code to:
    > |
    > | - Start 20 threads
    > | - End all threads after the sooner of 10 seconds or if all of the

    threads
    > | themselves have finished.
    > |
    > | BTW, each thread checks an email address for validity with
    > www.aspnetmx.com.
    > | If it is taking too long (e.g. > 10 seconds) I just want to look at the
    > | results for the threads that have finished.
    > |
    > | TIA
    > | Mark
    > |
    > |
    > |
    > |
    > |
    >
    >
    >
     
    Mark B, Jun 21, 2005
    #6
  7. Did you read my reply from last week? I spent a long time covering this. I
    recommended that you do not use multiple threads. Perhaps that isn't what
    you wanted to hear, but I gave you quite a few good reasons not to.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.

    "Mark B" <> wrote in message
    news:...
    > Thanks very much.
    >
    > I haven't really programmed much in VB.Net before just VBA.
    >
    > I couldn't find the method "BeginValidate" or "EndValidate" in the
    > ASPNetMX
    > documentation at http://www.aspnetmx.com/help/default.htm. Where does it
    > come from?
    >
    > Also, excuse my .Net ignorance but does "asynchronously" mean at the same
    > time?
    >
    > And, is "For index As Integer = 0 To 20 - 1" the same as:
    >
    > Dim index as Integer
    > For index = 0 To 19
    >
    >
    >
    > "Jay B. Harlow [MVP - Outlook]" <> wrote in message
    > news:...
    >> Mark,
    >> In addition to the other comments:
    >>
    >> Directly creating threads can be costly, rather then use New Thread, I

    > would
    >> recommend using either the ThreadPool, creating your own thread pool, or
    >> using an async delegate.
    >>
    >> The following MSDN Magazine article shows one method of creating a timed
    >> method.
    >>
    >> http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/default.aspx
    >>
    >>
    >> Depending on how you call www.aspnetmx.com, for example if its a Web

    > Service
    >> wouldn't it be easier to simply set the WebClientProtocol.Timeout
    >> property
    >> to your timeout amount (10 seconds)?
    >>
    >>

    > http://msdn.microsoft.com/library/d...otocolswebclientprotocolclasstimeouttopic.asp
    >>
    >> Something like:
    >>
    >> '********CODE START***************
    >>
    >> ' set the method timeout on the proxy to 10 seconds
    >> Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)
    >>
    >>
    >> Dim intResult(20) as Integer = 1
    >> Dim i as Integer
    >> 'intResult(i) 1 = Not Finished, 2 = True , 3 =False
    >>
    >> For i = 1 To 20
    >>
    >> '*******Start thread (i) here
    >> If Mx.Validate(Session("Email"+Format$(i,"00")))=True Then
    >> 'This tests the particular email address
    >> intResult(i)= 2
    >> Else
    >> intResult(i)= 3
    >> End If
    >> ' *********End thread (i)i here
    >>
    >> Next i
    >>
    >> '********CODE END***************
    >>
    >> Also, depending on how may total requests I had coming in, I would

    > consider
    >> doing these 20 outgoing requests asynchronously.
    >>
    >> Something like (syntax checked only):
    >>
    >> Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)
    >>
    >> Dim intResults(20 - 1) As Integer
    >>
    >> Dim results(20 - 1) As IAsyncResult
    >>
    >> ' Queue up all the requests
    >> For index As Integer = 0 To 20 - 1
    >>
    >> intResults(index) = 1
    >> results(index) =
    >> Mx.BeginValidate(Session("Email"+Format$(i,"00)), nothing, nothing)
    >>
    >> Next index
    >>
    >> ' wait for each request to finish
    >> For index As Integer = 0 To 20 - 1
    >> If Mx.EndValidate(results(index)) Then
    >> 'This tests the particular email address
    >> intResults(index)= 2
    >> Else
    >> intResults(index)= 3
    >> End If
    >> Next
    >>
    >> NOTE: You may need the Mx.EndValidate in a Try/Catch as the Timeout will
    >> cause an exception to be raised...
    >>
    >> The advantage of using BeginValidate is that all 20 validates are

    > executing
    >> concurrently...
    >>
    >> Hope this helps
    >> Jay
    >>
    >> "Mark B" <> wrote in message
    >> news:...
    >> | Hi
    >> |
    >> | I was wondering if someone could help me with the syntax for some
    >> VB.Net
    >> | ASPX code to:
    >> |
    >> | - Start 20 threads
    >> | - End all threads after the sooner of 10 seconds or if all of the

    > threads
    >> | themselves have finished.
    >> |
    >> | BTW, each thread checks an email address for validity with
    >> www.aspnetmx.com.
    >> | If it is taking too long (e.g. > 10 seconds) I just want to look at the
    >> | results for the threads that have finished.
    >> |
    >> | TIA
    >> | Mark
    >> |
    >> |
    >> |
    >> |
    >> |
    >>
    >>
    >>

    >
    >
     
    Kevin Spencer, Jun 21, 2005
    #7
  8. Mark,
    | I couldn't find the method "BeginValidate" or "EndValidate" in the
    ASPNetMX
    | documentation at http://www.aspnetmx.com/help/default.htm. Where does it
    | come from?
    "BeginValidate" or "EndValidate" are based on ASPNetMX being a Web Service,
    ASPNetMX is not a web service, ergo they don't exist per se. You should be
    able to use an asynchronous delegate with ASPNetMX.

    http://msdn.microsoft.com/library/d...s/cpguide/html/cpovrasynchronousdelegates.asp

    | Also, excuse my .Net ignorance but does "asynchronously" mean at the same
    | time?
    Yes. For details on asynchronous programming see:

    http://msdn.microsoft.com/library/d...cpguide/html/cpconasynchronousprogramming.asp


    | And, is "For index As Integer = 0 To 20 - 1" the same as:
    |
    | Dim index as Integer
    | For index = 0 To 19
    Yes

    | I haven't really programmed much in VB.Net before just VBA.

    Looking at http://www.aspnetmx.com/help/default.htm, I would consider using
    ValidateQty instead of Validate, as ValidateQty does a list of addresses.
    Then I would not worry about getting multi-threading & asynchronous
    programming to work. As Kevin suggests multi-threading is something to
    consider not using, as it is a very advanced topic that is easy to make
    things worse then better, although I have not specifically read Kevin's
    response... Asynchronous programming ("BeginValidate" or "EndValidate") are
    generally easier, however even then you need to be very careful as its still
    multi-threaded programming.

    Hope this helps
    Jay

    "Mark B" <> wrote in message
    news:...
    | Thanks very much.
    |
    | I haven't really programmed much in VB.Net before just VBA.
    |
    | I couldn't find the method "BeginValidate" or "EndValidate" in the
    ASPNetMX
    | documentation at http://www.aspnetmx.com/help/default.htm. Where does it
    | come from?
    |
    | Also, excuse my .Net ignorance but does "asynchronously" mean at the same
    | time?
    |
    | And, is "For index As Integer = 0 To 20 - 1" the same as:
    |
    | Dim index as Integer
    | For index = 0 To 19
    |
    |
    |
    | "Jay B. Harlow [MVP - Outlook]" <> wrote in message
    | news:...
    | > Mark,
    | > In addition to the other comments:
    | >
    | > Directly creating threads can be costly, rather then use New Thread, I
    | would
    | > recommend using either the ThreadPool, creating your own thread pool, or
    | > using an async delegate.
    | >
    | > The following MSDN Magazine article shows one method of creating a timed
    | > method.
    | >
    | > http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/default.aspx
    | >
    | >
    | > Depending on how you call www.aspnetmx.com, for example if its a Web
    | Service
    | > wouldn't it be easier to simply set the WebClientProtocol.Timeout
    property
    | > to your timeout amount (10 seconds)?
    | >
    | >
    |
    http://msdn.microsoft.com/library/d...otocolswebclientprotocolclasstimeouttopic.asp
    | >
    | > Something like:
    | >
    | > '********CODE START***************
    | >
    | > ' set the method timeout on the proxy to 10 seconds
    | > Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)
    | >
    | >
    | > Dim intResult(20) as Integer = 1
    | > Dim i as Integer
    | > 'intResult(i) 1 = Not Finished, 2 = True , 3 =False
    | >
    | > For i = 1 To 20
    | >
    | > '*******Start thread (i) here
    | > If Mx.Validate(Session("Email"+Format$(i,"00")))=True Then
    | > 'This tests the particular email address
    | > intResult(i)= 2
    | > Else
    | > intResult(i)= 3
    | > End If
    | > ' *********End thread (i)i here
    | >
    | > Next i
    | >
    | > '********CODE END***************
    | >
    | > Also, depending on how may total requests I had coming in, I would
    | consider
    | > doing these 20 outgoing requests asynchronously.
    | >
    | > Something like (syntax checked only):
    | >
    | > Mx.Timeout = CInt(TimeSpan.FromSeconds(10).TotalMilliseconds)
    | >
    | > Dim intResults(20 - 1) As Integer
    | >
    | > Dim results(20 - 1) As IAsyncResult
    | >
    | > ' Queue up all the requests
    | > For index As Integer = 0 To 20 - 1
    | >
    | > intResults(index) = 1
    | > results(index) =
    | > Mx.BeginValidate(Session("Email"+Format$(i,"00)), nothing, nothing)
    | >
    | > Next index
    | >
    | > ' wait for each request to finish
    | > For index As Integer = 0 To 20 - 1
    | > If Mx.EndValidate(results(index)) Then
    | > 'This tests the particular email address
    | > intResults(index)= 2
    | > Else
    | > intResults(index)= 3
    | > End If
    | > Next
    | >
    | > NOTE: You may need the Mx.EndValidate in a Try/Catch as the Timeout will
    | > cause an exception to be raised...
    | >
    | > The advantage of using BeginValidate is that all 20 validates are
    | executing
    | > concurrently...
    | >
    | > Hope this helps
    | > Jay
    | >
    | > "Mark B" <> wrote in message
    | > news:...
    | > | Hi
    | > |
    | > | I was wondering if someone could help me with the syntax for some
    VB.Net
    | > | ASPX code to:
    | > |
    | > | - Start 20 threads
    | > | - End all threads after the sooner of 10 seconds or if all of the
    | threads
    | > | themselves have finished.
    | > |
    | > | BTW, each thread checks an email address for validity with
    | > www.aspnetmx.com.
    | > | If it is taking too long (e.g. > 10 seconds) I just want to look at
    the
    | > | results for the threads that have finished.
    | > |
    | > | TIA
    | > | Mark
    | > |
    | > |
    | > |
    | > |
    | > |
    | >
    | >
    | >
    |
    |
     
    Jay B. Harlow [MVP - Outlook], Jun 21, 2005
    #8
    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. tshad
    Replies:
    7
    Views:
    17,277
    tshad
    Mar 11, 2005
  2. 00_CP_D12
    Replies:
    3
    Views:
    9,061
    dilipv
    Feb 22, 2008
  3. cppaddict
    Replies:
    20
    Views:
    849
    Chris Uppal
    Aug 11, 2004
  4. Stu
    Replies:
    7
    Views:
    1,826
    Dave Thompson
    Mar 7, 2005
  5. Michael Tan
    Replies:
    32
    Views:
    1,079
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page