Home > Software engineering >  Multithreading open the Excel in VB.net, then fill in the contents
Multithreading open the Excel in VB.net, then fill in the contents

Time:09-18

I want to be a little tool, I have a Excel file, the inside of the content must be read from the database to fill in to Excel in the corresponding cell, and generate multiple Excel file, because the quantity is big, adopts multi-thread method at the same time open five threads, but some machines to test a few Excel process there is no response, some machines can more output, but the way to a certain extent process also no response, is there any one great god studied, look at what is causing the,

CodePudding user response:

Have no way to directly target from your description to the problem, there are many reasons, too many threads, not released after resource use, or resources are exclusive access to conflict, even the computer resource depletion by jammed possible, overall I think it should be met is unusual, but caused no exception handling in your multithreaded thrown out (or while there is exception handling, but thanks to the failure of thread safety normal thrown),
If just want to get around this problem, suggest you to switch to NPOI,
Where in the world out if you want to dig into problems, it will be on the outer package of a layer of each thread code Try Catch, and then recorded in the Catch all exceptions,

CodePudding user response:

Problem description is too vague,

There are quite a few problems, it is others "guess" out, "case-by-case basis", if you can, best can put your code,

CodePudding user response:

Code is messy, adjusted, trouble two upstairs to see

The Public Module Mod_MultiThreads
Public Delegate Sub ProcessSub (ByVal pFile As Object, ByRef pWorker As Object) 'this is the final processing function
Public Delegate Sub MultiThreadsSub (ByVal pData As Object, ByVal pProcessFunc As ProcessSub) 'the Delegate is thread function directly, the correlation method should be written according to the actual situation and reference ServeForMultiThread methods in this class and write
Public Delegate Sub MultiThreadsSub2 (ByVal pData As Object) 'pData may pass here is that the data collection, data may be a single, pay attention to the judge, can be directly call a dictionary Object, the incoming parameters, it can also deal with multiple parameter
Public Dyn_Object As New Dynamic. ExpandoObject 'Dynamic objects, to dynamically add members and methods


"' & lt; summary>
'"' open the multi-thread functions, only to open multiple excel thread to handle the file
"' & lt;/summary>
"' & lt; Param name="pCollectObj & gt;" Data collection, & lt;/param>
"' & lt; Param name="pProcessSub & gt;" Specific data processing functions & lt;/param>
"' & lt; Param name="pGroupNum & gt;" Each group member number & lt;/param>
"' & lt; Param name="pThreadNum & gt;" Open the number of threads at a time, don't too big (less than 200), otherwise easy to cause the program to crash & lt;/param>
"' & lt; Remarks> 'note here, all data types in the collection of data will be converted to object, please type in the thread function transformation & lt;/remarks>
Public Sub StartMultiThreadsOnlyForExcel (ByVal pCollectObj As Object, ByVal pProcessSub As ProcessSub, Optional ByVal pGroupNum As Integer=30, Optional ByVal pThreadNum As Integer=30)

Dim m_lList As New List (Of Object)
Dim m_lZList As New List (Of the List (Of Object))
Try
If TypeOf pCollectObj Is OleDb. OleDbDataReader Then 'add this code to solve the problem of excessive cannot pass data collection,
The Do While pCollectObj. Read
M_lList. Add (pCollectObj. Item (0))
If m_lList. Count=pGroupNum Then
M_lZList. Add (m_lList)
M_lList=New List (Of Object)
End the If

If m_lZList. Count=pThreadNum Then

'open multithreaded TODO
The Parallel ForEach (m_lZList, Sub (ITem) ServeForMultiThread (ITem, pProcessSub))
'processing list, reduce memory pressure
For Each pList As List (Of Object) In m_lZList
PList. The Clear ()
PList=Nothing
Next
M_lZList. The Clear ()
End the If
Loop
The Else
For Each pMember As Object In pCollectObj
M_lList. Add (pMember)
If m_lList. Count=pGroupNum Then
M_lZList. Add (m_lList)
M_lList=New List (Of Object)
End the If

If m_lZList. Count=pThreadNum Then

'open multithreaded TODO
The Parallel ForEach (m_lZList, Sub (ITem) ServeForMultiThread (ITem, pProcessSub))
'processing list, reduce memory pressure
For Each pList As List (Of Object) In m_lZList
PList. The Clear ()
PList=Nothing
Next
M_lZList. The Clear ()
End the If

Next
End the If


If m_lZList. Count & gt; 0 Then

'TODO
The Parallel ForEach (m_lZList, Sub (ITem) ServeForMultiThread (ITem, pProcessSub))
For Each pList As List (Of Object) In m_lZList
PList. The Clear ()
PList=Nothing
Next
End the If

If m_lList. Count & gt; 0 Then

'TODO
The Parallel ForEach (m_lList, Sub (ITem) ServeForMultiThread (ITem, pProcessSub))
M_lList. The Clear ()
M_lList=Nothing
End the If


Catch the ex As Exception
GYPublicInfo. PublicInfo. ErrCl (ex. ToString ())
The Finally
If Not m_lZList Is Nothing Then
M_lZList. The Clear ()
M_lZList=Nothing
End the If

If Not m_lList Is Nothing Then
M_lList. The Clear ()
M_lList=Nothing
End the If
End the Try



End Sub

End the Module


Private Function OutputDCB_MultiThread_HuNanSheng (ByVal ListBM As List (Of String)) As a Boolean
Dim m_lDCB_Print As ProcessSub=AddressOf Single_OutputDCB
Dim m_lGroupNum As Integer=30
Dim m_lThreadsNum As Integer=30

StartMultiThreadsOnlyForExcel (ListBM m_lDCB_Print, m_lGroupNum m_lThreadsNum)
Return True
End the Function


Private Function Single_OutputDCB (ByVal SingleCBFBM As String, ByRef pPutOutExcel As GY. GNP. CPutOutExcel) As Boolean
Dim ModulePath As String
Dim _OutputFullpath As String

Try
If _OutputFolderType. ToUpper="CBF" Then
Dim locked1 As Boolean=False
Try
GYPublicInfo. PublicInfo. Locker. Enter (locked1)

If System. IO. Directory. The Exists (_OutputFolder & amp; "" & amp; SingleCBFBM. Substring (0, 14) & amp; "" & amp; SingleCBFBM & amp; "")=False Then
System. IO. Directory. CreateDirectory (_OutputFolder & amp; "" & amp; SingleCBFBM. Substring (0, 14) & amp; "" & amp; SingleCBFBM & amp; "")
End the If
Catch the ex As Exception
The Finally
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related