G
Guest
I am having a problem with QueueUserWorkItem. I thought there was a maximum
number of worker threads a threadpool had available, 25 per processor, or
something like that. However, when executing the following code, after about
125 or so Excel processes are running I get:
"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in Fiscal Year
End.exe"
Additional information: Server execution failed
When another thread starts to execute. I would expect at most 50 concurrent
Excel threads on a P4 hyperthreading system. Is there something wrong in my
code that keeps the system from recognizing the maximum thread count?
Private ExcelPool As System.Threading.ThreadPool
<Some Code>
For Each AccountRow In ReportingDS.Tables("Ledger Accounts").Rows
SummaryRows = AccountRow.GetChildRows("Account Summary")
For Each SummaryRow In SummaryRows
TargetRange = WS.Cells(CurrentRow, 1)
TargetRange.Resize(1, _
SummaryRow.ItemArray.GetLength(0)).Value = SummaryRow.ItemArray
TargetRange = WS.Cells(CurrentRow, 1)
WS.Hyperlinks.Add(TargetRange, SaveSubDirectoryString & _
WS.Cells(CurrentRow, 1).Value & ".xls", , _
"Click on account number to open account activity.", _
WS.Cells(CurrentRow, 1).Value)
CurrentRow += 1
Next
Dim StObj As New StateObj
StObj.DataRowArg = AccountRow.GetChildRows("Account Detail")
StObj.DataTableArg = ReportingDS.Tables("Ledger Detail").Clone
StObj.SavePath = SaveSubDirectoryString & AccountRow(0) + ".xls"
StObj.DSName = AccountRow(0)
ExcelPool.QueueUserWorkItem(New System.Threading.WaitCallback _
(AddressOf SaveExcelData), StObj)
Next
<More Code>
Private Sub SaveExcelData(ByVal StateObj As Object)
Dim StObj As StateObj
StObj = CType(StateObj, StateObj)
Dim ThreadExcel As New Excel.Application
Dim ThreadWorkbook As Excel.Workbook = ThreadExcel.Workbooks.Add
While ThreadWorkbook.Worksheets.Count > 1
ThreadWorkbook.Worksheets(ThreadWorkbook.Worksheets.Count).Delete()
End While
Dim ThreadWorksheet As Excel.Worksheet = ThreadWorkbook.Worksheets(1)
Dim DR As DataRow
Dim DC As DataColumn
For Each DC In StObj.DataTableArg.Columns
ThreadWorksheet.Cells(1, DC.Ordinal + 1) = DC.ColumnName
Next
ThreadWorksheet.Rows(1).Font.Bold = True
ThreadWorksheet.Rows(1).Font.Underline =
Excel.XlUnderlineStyle.xlUnderlineStyleSingle
Dim CurrentRow As Integer = 2
Try
For Each DR In StObj.DataRowArg
For Each DC In StObj.DataTableArg.Columns
ThreadWorksheet.Cells(CurrentRow, DC.Ordinal + 1).Value =
DR(DC.Ordinal)
Next
CurrentRow += 1
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Thread Excel")
ThreadWorksheet = Nothing
ThreadWorkbook = Nothing
ThreadExcel.Quit()
ThreadExcel = Nothing
End Try
ThreadWorkbook.SaveAs(StObj.SavePath)
ThreadWorkbook.Close()
ThreadExcel.Quit()
ThreadExcel = Nothing
StObj = Nothing
End Sub
number of worker threads a threadpool had available, 25 per processor, or
something like that. However, when executing the following code, after about
125 or so Excel processes are running I get:
"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in Fiscal Year
End.exe"
Additional information: Server execution failed
When another thread starts to execute. I would expect at most 50 concurrent
Excel threads on a P4 hyperthreading system. Is there something wrong in my
code that keeps the system from recognizing the maximum thread count?
Private ExcelPool As System.Threading.ThreadPool
<Some Code>
For Each AccountRow In ReportingDS.Tables("Ledger Accounts").Rows
SummaryRows = AccountRow.GetChildRows("Account Summary")
For Each SummaryRow In SummaryRows
TargetRange = WS.Cells(CurrentRow, 1)
TargetRange.Resize(1, _
SummaryRow.ItemArray.GetLength(0)).Value = SummaryRow.ItemArray
TargetRange = WS.Cells(CurrentRow, 1)
WS.Hyperlinks.Add(TargetRange, SaveSubDirectoryString & _
WS.Cells(CurrentRow, 1).Value & ".xls", , _
"Click on account number to open account activity.", _
WS.Cells(CurrentRow, 1).Value)
CurrentRow += 1
Next
Dim StObj As New StateObj
StObj.DataRowArg = AccountRow.GetChildRows("Account Detail")
StObj.DataTableArg = ReportingDS.Tables("Ledger Detail").Clone
StObj.SavePath = SaveSubDirectoryString & AccountRow(0) + ".xls"
StObj.DSName = AccountRow(0)
ExcelPool.QueueUserWorkItem(New System.Threading.WaitCallback _
(AddressOf SaveExcelData), StObj)
Next
<More Code>
Private Sub SaveExcelData(ByVal StateObj As Object)
Dim StObj As StateObj
StObj = CType(StateObj, StateObj)
Dim ThreadExcel As New Excel.Application
Dim ThreadWorkbook As Excel.Workbook = ThreadExcel.Workbooks.Add
While ThreadWorkbook.Worksheets.Count > 1
ThreadWorkbook.Worksheets(ThreadWorkbook.Worksheets.Count).Delete()
End While
Dim ThreadWorksheet As Excel.Worksheet = ThreadWorkbook.Worksheets(1)
Dim DR As DataRow
Dim DC As DataColumn
For Each DC In StObj.DataTableArg.Columns
ThreadWorksheet.Cells(1, DC.Ordinal + 1) = DC.ColumnName
Next
ThreadWorksheet.Rows(1).Font.Bold = True
ThreadWorksheet.Rows(1).Font.Underline =
Excel.XlUnderlineStyle.xlUnderlineStyleSingle
Dim CurrentRow As Integer = 2
Try
For Each DR In StObj.DataRowArg
For Each DC In StObj.DataTableArg.Columns
ThreadWorksheet.Cells(CurrentRow, DC.Ordinal + 1).Value =
DR(DC.Ordinal)
Next
CurrentRow += 1
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Thread Excel")
ThreadWorksheet = Nothing
ThreadWorkbook = Nothing
ThreadExcel.Quit()
ThreadExcel = Nothing
End Try
ThreadWorkbook.SaveAs(StObj.SavePath)
ThreadWorkbook.Close()
ThreadExcel.Quit()
ThreadExcel = Nothing
StObj = Nothing
End Sub