473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1939
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.