Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Threads in CLR

$
0
0

I have a CLR table function that works fine but trying to improve its performance by using some threading.

I have the following code working but my WaitAll doesn't work when it is compiled and ran from SQL, sometimes it returns all of the data from all of the threads and sometimes I only get part of the records.

Any ideas on how I can be sure all of the threads have returned their rows before I go on? The GetBOMListingNew returns a list of parts which is making SQL calls, etc. If I run it in the debugger, after the Task.WaitAll, I can see some of the data updating in my debugging variables...so it works, it is just that I can't figure out how to be sure all of the threads are finished. 

I have done all of the settings for the Unsafe permissions, etc.


            For i As Int32 = 0 To n
                Dim ChildConnection = New SqlConnection(_inputOptions.IntegratedSecurityConnectionString)
                ConnectionArray(i) = ChildConnection
                ChildConnection.Open()
                objBomid = objBomidList(i)
                TaskPartsArray(i) = Task(Of List(Of Part)).Factory.StartNew(Function() BOMThread.GetBomListingNEW(objPart _
                                  , objBomid _
                                  , strRanking _
                                  , strOriginalRanking _
                                  , objPart.DeltaXOutput _
                                  , objPart.DeltaYOutput _
                                  , objPart.DeltaZOutput _
                                  , _inputOptions _
                                  , _lineGroupItemOptionsList _
                                  , _partPriceAggregateList _
                                  , _autoRuleList _
                                  , _partAttributeList _
                                  , _partAttributeMethodList _
                                  , _lineGroupItemOptionRankingList _
                                  , _completePartList _
                                  , _processStepsList _
                                  , _spList _
                                  , ChildConnection _
                                  ) _
                    )
                TaskList(i) = TaskPartsArray(i)

            Next

            Try
                Task.WaitAll(TaskList)
                'Thread.Sleep(500)
                For ii As Int32 = 0 To n
                        TaskPartsList = TaskPartsArray(ii).Result
                        objBomList.AddRange(TaskPartsList)
                Next

                For ii As Int32 = 0 To n
                    ConnectionArray(ii).Close()
                Next
            Catch ex As Exception
                Dim i As Integer
                Dim strErr As String = ex.Message
                Dim objPartErr As Part
                objPartErr = AddPartAsSqlErrorMsg(strErr, _inputOptions)
                objBomList.Add(objPartErr)
                i = 0
            End Try


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>