472,373 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Call the dispose method on a different thread if an exception is thrown

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
Nov 20 '05 #1
3 1839
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]
Nov 20 '05 #2
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]

Nov 20 '05 #3
On 2003-10-16, Giovanni Bassi <gb****@coair.com> wrote:
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


Giovanni,

Looking over the code, assuming I understand what's happening :), I
would suggest you look into using an async delegate to call a method
that generates the report... I think you will save your self a lot of
trouble, and end up with the same results. Basically, what you would do
is create and call a method, called say GenerateReports, using async
delegate. The GenerateReports method would be as simple as:

Private Delegate Sub GenReportsDelegate()

' menu click...
Private Sub...
Dim async As New GenReportsDelegate(AddressOf Me.GenerateReports)

' now call the method async like...
async.BeginInvoke(AddressOf Me.GenerateReportsComplete, Nothing)
End Sub

' Generate your reports in the background...
Private Sub GenerateReports()
Dim objReports As Reports = _
New ExcelPayrollCD(strFullFileName, g_objTables.Connection, intSerial)

AddHandler objReports.ReportFinished, AddressOf objReports_ReportFinished

Try
objReports.Generate()
Catch
' do whatever
Finally
objReports.Dispose()
End Try
End Sub

' do this so you make sure EndInvoke is called
Private Sub GenerateReportsDone(ByVal ar As IAsyncResult)
Dim async As GenReportsDelegate = _
DirectCast(ar.AsyncState, GenReportsDelegate)

async.EndInvoke(ar)
End Sub

What will change, and I think for the better is that you will eliminate
all the explicit threading, since the system will manage the threads.
Not only that, in the end the code is simpler. I don't have the docs on
this machine to give you a reference in them, but you can find a pretty
good overview in the docs on MSDN...

http://msdn.microsoft.com/library/de...rogramming.asp

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jakob Bengtsson | last post by:
Hi I'm trying to show a form as a dialog (using the .ShowDialog() method). After showing the dialog, I want to dispose of the form I use this design (simplified to clarify the point) ...
14
by: Daniel Billingsley | last post by:
The example code for Memory shows it being used in a using() block. The documentation for using() says it can only be used on things that implement Disposable. Yet there is no Dispose() method...
10
by: Clint | last post by:
Hey all - I'm having a really confusing problem concerning a web service. Right now, I have an application that needs to call a web service that does nothing but return "true" (this will...
0
by: Joe | last post by:
Reposting here as there were no useful replies in the dotnet.framework NG... What is the correct pattern for handling exceptions in IDisposable.Dispose, especially in a class that manages...
1
by: Sagaert Johan | last post by:
Hi Ii have a simple server thread in an app that listens for connections, for some unclear reason an exception is thrown every now and then : 'A blocking operation was interrupted by a call to...
9
by: Menny | last post by:
Hi, I'm looking for a way to determine if the 'Dispose()' function at the end of a 'using' block, was called due to an exception. Can anyone help?
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
3
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.