I've a class A that implements IDisposable interface and has a method that
start a thread.
In Dispose(bool) can I use the lock keyword to wait for thread exit before
disposing resources it use or it's enough to pay attention when disposing
objects of type A from "consumer" classes?
Thanks,
Luigi.
May 19 '07
11 9136
"Moty Michaely" <Mo*****@gmail. comschrieb im Newsbeitrag
news:11******** **************@ r3g2000prh.goog legroups.com...
On May 21, 12:47 pm, "Christof Nordiek" <c...@nospam.de wrote:
>"Moty Michaely" <Moty...@gmail. comschrieb im Newsbeitragnew s:11*********** ***********@n15 g2000prd.google groups.com...
>So, the if(disposing) statement would never be entered, when the Finalizer is called. And Dispose of components shouldn't be called by the Finalizer anyway.
>Christof
Hi,
What if things should be done without trusting the developer to call
Dispose?
Then the compononents (and their components ....) themselves are eligable for finalization will be cleaned up by their own finalizer. So calling Dispose isn't necessary anymore. That's way classes like Form and Component implement the Dispose(bool) method. By this inheriting classes can easily implement the cleanup of managed resources, wich should only occur during disposing, and cleanup of managed resources, wich has to occur during disposing aswell as during finalization .
Christof
But again,
What if the I need to serialize a class when it's about to be
disposed?
I would do it in the finalizer, right? (with dispose(false))
No, this is not a good place for such things.
Finalizer should only clean up unmanaged resources, nothing else.
Also Dispose is also not a good place for serialization. It should only
cleanup managed and unmanged resources.
If you use Dispose for storing data or something else. Then you should
clearly communicate that to the caller of the class, and make it clear, that
data may be lost, if Dispose isn't called.
I don't know the scenario, you are talking about, but I guess, the is a
better way (like implementing an ISaveable interface or so).
Christof
You can use a lock() (or a ReaderWriterLoc k, etc) in a Dispose() method.
There's no trouble with this at all.
A Dispose method, after all, is just a standard method running on the thread
of whatever called Dispose().
Where you really, really don't want to use a lock is inside a Finalizer. The
CLR uses a single thread to call finalizers, and if you block this thread,
finalization cannot happen and your application is now screwed.
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
"BLUE" <bluewrote in message news:OT******** ******@TK2MSFTN GP04.phx.gbl...
I've a class A that implements IDisposable interface and has a method that
start a thread.
In Dispose(bool) can I use the lock keyword to wait for thread exit before
disposing resources it use or it's enough to pay attention when disposing
objects of type A from "consumer" classes?
Thanks,
Luigi.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ken Durden |
last post by:
I am in search of a comprehensive methodology of using these two
object cleanup approaches to get rid of a number of bugs,
unpleasantries, and cleanup-ordering issues we currently have in our
4-month old C#/MC++ .NET project project.
I'd like to thank in advance anyone who takes the time to read and/or
respond to this message. At a couple points, it may seem like a rant
against C# / .NET, but we are pretty firmly stuck with this approach...
|
by: Andrew Dowding |
last post by:
Hi Everybody,
I have been looking at problems with my Windows Forms C# application
and it's little Jet 4 (Access) database for the last few days. The
Windows Forms app implements a facade and implementation, data
abstraction layer. But because each data adapter in the implementation
layer has a connection object that opens and closes as needed, I found
I got several errors from the Jet engine when there were simultaneous
connections to...
|
by: Dennis |
last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose
method based on advice from this newsgroup. However, I'm not sure how to
dispose of the following object that was created inside a method call.
dim myvar as new object1
object1.dosomethingmethod(new object2)
Note that object 2 has a dispose method but how do I dispose of it unless I
do the following:
|
by: Don |
last post by:
How to stop a process which is running in a separate thread!!!
I've got a class which performs some lengthy process in a background
(separate) thread. And this lengthy process raises events regularly to inform
the main thread of the progress (which is then displayed to the user).
Since the event is raised from another thread, i've used me.Invoke() to
update the ui properly.
However, my problem is in cancelling the process.
I need to...
|
by: Gina_Marano |
last post by:
Hey All,
I am using multiple child threads per main thread to download files.
It sometimes appears as if the same file is being downloaded twice. I
am using "lock". Am I using it correctly? Any blantant threading
errors here?
Any opinions would be greatly appreciated.
namespace MainThreadManagement
| |
by: not_a_commie |
last post by:
I'd like to be able to overload the lock command so that I could log
an entry on lock and unlock. Any ideas on how to do this?
I think it would be powerful if you could inherit from a few keywords
in C# like this example:
static keyword MyLock : lock {
public MyLock(object obj) : base(obj) { log a message when entering
scope }
public ~MyLock() { log a message when going out of scope }
|
by: snowbunny |
last post by:
Hi,
I'm having a problem with releasing a file lock after a save. My code takes an image file uploaded via a form, and saves it to disk. That's fine, but I'm trying to add a new function which allows the user to choose to automatically create a thumbnail. At this stage, it throws a "System.OutOfMemoryException: Out of memory." Even if the files are small, I still get this, and I find that if I open up Windows Explorer and try to delete the...
|
by: jeevankodali |
last post by:
Hi
I have a multi threaded ftp server. 2 days back it threw an exception:
System.IO.IOException: The specified network name is no longer
available.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream.WriteCore(Byte buffer, Int32 offset,
Int32 count)
at System.IO.FileStream.FlushWrite()
|
by: Clive Dixon |
last post by:
Are there any issues I should be worried about when using C# 2.0 iterators
in conjunction with lock, i.e.
public IEnumerator GetEnumerator()
{
lock (lockObject)
{
foreach (object obj in collection)
{
yield return obj;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |