Hey Tom,
Thanks for the Reply.
Here is what is some code:
This runs on a menu click handler:
Dim objReports As Reports
objReports = New ExcelPayrollCD(strFullFileName,
g_objTables.Connection, intSerial)
AddHandler objReports.ReportFinished, AddressOf
objReports_ReportFinished
Dim objNewThreadStart As New Threading.ThreadStart(AddressOf
objReports.Generate)
Dim objNewThread As New Threading.Thread(objNewThreadStart)
objNewThread.Start()
And there is this sub to handle the above code:
Private Sub objReports_ReportFinished(ByVal sender As Excel.Reports)
Dim Result As Excel.ReportResults
Result = sender.ReportResult
Dim objNewThreadStart As New Threading.ThreadStart(AddressOf
CType(sender, IDisposable).Dispose)
Dim objNewThread As New Threading.Thread(objNewThreadStart)
objNewThread.Start()
Select Case Result
Case Excel.ReportResults.OK
MessageBox.Show("Report created!" & vbNewLine &
sender.FullFileName & " saved.", "Payroll CD", MessageBoxButtons.OK,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Case Excel.ReportResults.Cancelled
MessageBox.Show("Report cancelled.")
Case Excel.ReportResults.ConnectionTimeOut
MessageBox.Show("Connection Timed Out.")
Case Excel.ReportResults.DataNotRetrived
MessageBox.Show("Data Not Retrived.")
Case Excel.ReportResults.ReportModelNotFound
MessageBox.Show("Report model not found.")
Case Excel.ReportResults.UnknownError
MessageBox.Show("Unkown Error.")
Case Else
End Select
End Sub
I want to catch an error on objReports.Generate, which is run at
objNewThread.Start.
Normally, without MultiThreading, I'd simply do:
Try
objReports.Generate()
Catch
'do whatever
Finally
objReports.Dispose
End Try
but I can't because there is no direct call to objReports.Generate, and
objReports.Dispose only runs when Sub objReports_ReportFinished gets called.
Any ideas?
Thanks again!
Giovanni Bassi
"Tom Shelton" <to*@mtogden.com> wrote in message
news:ue*************@TK2MSFTNGP11.phx.gbl...
On 2003-10-15, Giovanni Bassi <gb****@coair.com> wrote: Hello Group,
I am running an operation in a different thread. There are resources
that are released when the thread is done running. This is done at the end of
the execution as it raises an event, and then the operation handling this
event calls threaded object's dispose method.
The problem is: If an exception is thrown the event is never raised, the
operation never executes dispose and my resources get stuck on the
memory until the app is finally closed and then, just then, the finalize method
will call the dispose method.
How can I know if an exception was thrown in a different thread? I need
that to be able to call the dispose method myself and not wait until finalize
runs whenever it feels like.
Thanks in advance for all the replies,
Giovanni Bassi
I think you may want to check into Try-Catch-Finally... The finally
block is always executed - even if an exception is thrown. I'm not
exactly sure with out seeing some code of course, but I would guess this
is a code structure issue. Is there any way you can post a scaled down
snippet of code that illustrates what you are trying to accomplish?
--
Tom Shelton
MVP [Visual Basic]