Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Controlling Threads

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Richard Welch - 25 Jun 2005 03:10 GMT
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

Signature

Richard A. Welch
IT Manager
House of Raeford Farms, Inc. - Raeford Division

likwoka@gmail.com - 25 Jun 2005 05:31 GMT
I think it is about 25 thread per process (you can have a few processes
running on a single processor machine, and each process can have at
most 25 concurrent threads using the default ThreadPool.

Would this help?
Richard Welch - 25 Jun 2005 06:01 GMT
So, the program I wrote can have 25 threads in the threadpool? I am a bit
confused as to what constitues a process. As you can probably tell I am new
at multithreaded applications.
Signature

Richard A. Welch
IT Manager
House of Raeford Farms, Inc. - Raeford Division

> I think it is about 25 thread per process (you can have a few processes
> running on a single processor machine, and each process can have at
> most 25 concurrent threads using the default ThreadPool.
>
> Would this help?
Willy Denoyette [MVP] - 25 Jun 2005 11:21 GMT
Why do you have 125 Excel processes running in the first place? Do you mean
you create an instance of Excel in each worker thread you start? Well this
is wrong, you should only create a single Excel instance in your main thread
and you should not use any other thread at all.

Willy.

>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
[quoted text clipped - 82 lines]
>    StObj = Nothing
> End Sub
Richard Welch - 25 Jun 2005 14:58 GMT
Willy,
  I agree, it seems definately wrong to have to do multiple instances of
Excel to accomplish what I need to do. If there is a way to create between 3
and 4 thousand different workbooks quicker, I would definately like to know.  
I am trying to multithread to take advantage of the hyperthreading availalbe.
The process I am trying to write takes over 30 hours to accomplish in a
single Excel instance. I would like to be able to hold it down to a smaller
number of threads, but am unsure how to accomplish this.
Signature

Richard A. Welch
IT Manager
House of Raeford Farms, Inc. - Raeford Division

> Why do you have 125 Excel processes running in the first place? Do you mean
> you create an instance of Excel in each worker thread you start? Well this
[quoted text clipped - 89 lines]
> >    StObj = Nothing
> > End Sub
Lenn - 22 Aug 2005 21:51 GMT
A word of advice; Do NOT mix Excel Automation with .NET Framework 1.1
runtime. Especially for what you are trying to do. Once an instances of excel
is created, it becomes very 'difficult' to release it effectively. Anything
you 'touch' or instantiate under that instance; Workbook, Worksheet, cell,
etc. will hold a reference to COM. and you have to kill every single
Workbook, worksheet, cell to get rid of Excel process. You will find lots of
threads in this and other newsgroups regarding this issue. Also doing COM
Interop, and that's what you're doing with Excel, will not yeild the best
performance results, it seems you really care about that. No number of
threads will help you with that, if anything they will make it worse by
deadlocking.
I had to develop a very similar application, I ran into so many problems
with Excel and .NET, that I finally gave up on it, and developed a solution
in good old Vb6 and a few VBA Excel Macros. You could probably do the same
thing.

> Willy,
>    I agree, it seems definately wrong to have to do multiple instances of
[quoted text clipped - 98 lines]
> > >    StObj = Nothing
> > > End Sub

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.