Web Service and Threads

Ask a Question related to ASP.NET Web Services, Design and Development.

  1. #1

    Default Web Service and Threads

    What I'm trying to do is call a web service which then invokes a worker
    thread. This thread should then handle any further lengthy processsing
    freeing up the WebService to complete and terminiate.

    To do this I'm using code similar

    In the Client application I call

    Public Sub InvokeWebService
    Dim WS As New WS.Xfer

    Try
    Console.Write("Started")
    Dim cb As AsyncCallback = New AsyncCallback(AddressOf MyCallback)
    Dim ar As IAsyncResult = WS.BeginXfer(LogInf, cb, Nothing)
    Catch e As Exception
    Console.WriteLine("Exception Occured: " & e.ToString())
    End Try
    End Sub

    Public Sub MyCallback(ByVal ar As IAsyncResult)
    Dim WS As WS.Xfer = DirectCast(ar.AsyncState, WS.Xfer)
    Dim ReturnValue As String = WS.EndXfer(ar)
    Console.Write("Finished")
    End Sub

    Then in the web service I have the following

    <WebMethod(Description:="Do Lengthy Processing.")> _
    Public Function Xfer(ByVal newLogInfo As LogInfo) As Boolean
    Return DoSomething(newLogInfo)
    End Function

    Private Function DoSomething(ByVal LogInfo As LogInfo) As Boolean
    Dim st As New ThrdState
    st.mLogInfo = LogInfo

    ...

    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf MyRefresh),
    st)

    ...

    Return True
    End Function

    ' CALLBACK
    Private Sub MyRefresh(ByVal state As Object)
    Dim st As ThrdState = CType(state, ThrdState)
    Dim LogInfo As LogInfo = st.mLogInfo

    ...
    Do lengthy Processing here
    ...
    End Sub

    The problem is that when the web service completes the spawned thread
    terminiates. What I want is for the Web Service to complete and the
    spawned thread continue processing in the background. The Web Service
    doesn't care what now happens in the spawned thread.

    Any help greatly appreciated

    wbarr@tin-man.co.uk Guest

  2. Similar Questions and Discussions

    1. Debugging Threads
      Hi all, people have problems debugging threads. What do others think about, setting abort_on_exception to true if ruby's command line option -d...
    2. Need a bit of help with threads programming
      Hi all, I need a subroutine that reads the next line in an open file. Ordinarily this would be a trivial subroutine... you just read a line off...
    3. Threads troubles
      Hi, I am trying to write a multiuser server (for games, chat, etc) in Perl. In time, this program might have to service >500 users...
    4. Saving Threads
      Is there a way to save all the messages in a thread of do you have to save them from the forum one at a time? Dick -- Using M2, Opera's...
    5. Using threads to obtain a value
      I have a few classes that all try to obtain the same data as each other using different methods. Each one takes around 5 seconds to get the data,...
  3. #2

    Default Re: Web Service and Threads

    You should look into making your WebService an Async Handler:

    [url]http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx[/url]

    -Brock
    DevelopMentor
    [url]http://staff.develop.com/ballen[/url]


    > What I'm trying to do is call a web service which then invokes a
    > worker thread. This thread should then handle any further lengthy
    > processsing freeing up the WebService to complete and terminiate.
    >
    > To do this I'm using code similar
    >
    > In the Client application I call
    >
    > Public Sub InvokeWebService
    > Dim WS As New WS.Xfer
    > Try
    > Console.Write("Started")
    > Dim cb As AsyncCallback = New AsyncCallback(AddressOf MyCallback)
    > Dim ar As IAsyncResult = WS.BeginXfer(LogInf, cb, Nothing)
    > Catch e As Exception
    > Console.WriteLine("Exception Occured: " & e.ToString())
    > End Try
    > End Sub
    > Public Sub MyCallback(ByVal ar As IAsyncResult)
    > Dim WS As WS.Xfer = DirectCast(ar.AsyncState, WS.Xfer)
    > Dim ReturnValue As String = WS.EndXfer(ar)
    > Console.Write("Finished")
    > End Sub
    > Then in the web service I have the following
    >
    > <WebMethod(Description:="Do Lengthy Processing.")> _
    > Public Function Xfer(ByVal newLogInfo As LogInfo) As Boolean
    > Return DoSomething(newLogInfo)
    > End Function
    > Private Function DoSomething(ByVal LogInfo As LogInfo) As Boolean
    > Dim st As New ThrdState
    > st.mLogInfo = LogInfo
    > ...
    >
    > ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf MyRefresh),
    > st)
    >
    > ...
    >
    > Return True
    > End Function
    > ' CALLBACK
    > Private Sub MyRefresh(ByVal state As Object)
    > Dim st As ThrdState = CType(state, ThrdState)
    > Dim LogInfo As LogInfo = st.mLogInfo
    > ...
    > Do lengthy Processing here
    > ...
    > End Sub
    > The problem is that when the web service completes the spawned thread
    > terminiates. What I want is for the Web Service to complete and the
    > spawned thread continue processing in the background. The Web Service
    > doesn't care what now happens in the spawned thread.
    >
    > Any help greatly appreciated
    >


    Brock Allen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139