首页 > 解决方案 > 如何返回所有线程都在函数中完成?

问题描述

我有这段代码,它通过遍历字符串列表来启动一些线程,并将每个线程发送到Sub连接到 web 服务并等待结果的 a:

Public Shared Function StartThreading(names As String())
        Dim threads As List(Of Thread) = New List(Of Thread)()
        For Each name In names
            Dim t As Thread = New Thread(New ParameterizedThreadStart(Sub() CallWebService(name)))
            threads.Add(t)
        Next
        For i As Integer = 0 To threads.Count - 1
            Dim t = threads(i)
            Dim name = names(i)
            t.Start(name)
        Next
        For Each t As Thread In threads
            t.Join()
        Next
    End Function

Web 服务调用者的 Sub 是:

Public Shared Sub CallWebService(inputxml As String)
        Dim _url = "http://10.231.58.173:8080/ps/services/ProcessServer?WSDL"
        Dim _action = "http://10.231.58.173:8080/ps/services/ProcessServer"
        Dim soapEnvelopeXml As XmlDocument = CreateSoapEnvelope(inputxml)
        Dim webRequest As HttpWebRequest = CreateWebRequest(_url, _action)
            Dim appPath As String = System.AppDomain.CurrentDomain.BaseDirectory
            Dim configpath As String = appPath + "\Config.ini"
            Dim configdata As IniData = Functii.ReadIniFile(configpath)
            Dim outputxmlsave = configdata("PATHS")("OUTPUTXML")
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest)
            Dim asyncResult As IAsyncResult = webRequest.BeginGetResponse(Nothing, Nothing)
            asyncResult.AsyncWaitHandle.WaitOne()
            Dim soapResult As String
        Using webResponse As WebResponse = webRequest.EndGetResponse(asyncResult)
            Using rd As StreamReader = New StreamReader(webResponse.GetResponseStream())
                soapResult = rd.ReadToEnd()
            End Using
            File.WriteAllText(outputxmlsave & "\" & "test.xml", soapResult.ToString)
            Console.Write(soapResult)
        End Using
    End Sub

我怎么知道所有线程是否成功完成?True如果它们都完成了,我可以用什么来返回一个值吗?

标签: vb.netmultithreadingweb-services

解决方案


推荐阅读