473,796 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can lock into a dispose?

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 #1
11 9134
Luigi,

The use of lock is discouraged in a finalizer:

http://blogs.msdn.com/yunjin/archive...05/435726.aspx

On top of that, if you take too long, the possibility exists that your
finalizer will be shut down anyways if it is the result of an app domain
shutting down:

http://www.bluebytesoftware.com/blog...600869018.aspx

I would recommend that you have a method that you call to wait for the
thread to terminate, then dispose of your object.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"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.

May 19 '07 #2
It's a big big problem!

My GUI has a timer that is used to trigger data sync with a WS: when the
timer elapses it calls a method of the BLL which in turn call Sync method of
the DAL.
Sync create a thread that will synchronize data with the WS raising an event
for each step of the sync process (even an error).

Each layer (except for the presentation one) is a singleton class that
implements IDisposable interface.

So in the Closing event of the form, but also in the Dispose method I'll
call bll.Dispose() and in the BLL.Dispose method I'll call dal.Dispose().
Now in the case the timer has just elapsed and the user decide to close the
app I must wait sync to end, but how?
I could call a method in the closing event to disable the timer and I must
wait the last event (dataSynced) or the error event but how to stop closing
until synchronization ?
I suppose I cannot make the user wait for a long time since it's not so good
and I cannot abort the thread since I'm using CF 1.x on WinCE 4.2!
Moreover this wait cannot be done in the form Dispose so it will never
dispose the dal!
Thank you for any help!
Luigi.
May 19 '07 #3
I am curious, why are you going to have a singleton class that
implements IDispose? Those should be instances, because you are implying
that they have an explicit lifetime, which runs somewhat contrary to what a
singleton is.

You are going to have to make some sort of mechanism to cancel the
operation on the other thread in the case you want to shut down. You really
shouldn't be handling this in the Dispose method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"BLUE" <bluewrote in message
news:u%******** **********@TK2M SFTNGP04.phx.gb l...
It's a big big problem!

My GUI has a timer that is used to trigger data sync with a WS: when the
timer elapses it calls a method of the BLL which in turn call Sync method
of the DAL.
Sync create a thread that will synchronize data with the WS raising an
event for each step of the sync process (even an error).

Each layer (except for the presentation one) is a singleton class that
implements IDisposable interface.

So in the Closing event of the form, but also in the Dispose method I'll
call bll.Dispose() and in the BLL.Dispose method I'll call dal.Dispose().
Now in the case the timer has just elapsed and the user decide to close
the app I must wait sync to end, but how?
I could call a method in the closing event to disable the timer and I must
wait the last event (dataSynced) or the error event but how to stop
closing until synchronization ?
I suppose I cannot make the user wait for a long time since it's not so
good and I cannot abort the thread since I'm using CF 1.x on WinCE 4.2!
Moreover this wait cannot be done in the form Dispose so it will never
dispose the dal!
Thank you for any help!
Luigi.
May 19 '07 #4
I am curious, why are you going to have a singleton class that implements
IDispose?
Since I have a class that use an IntPtr and WinCE API calls to detect
insertion into the cradle I have to implement the dispose pattern
(dispose+finali ze) for this singleton class: how to dispose unmanged
resources else?

I suppose the lifetime of all my singleton objects is the lifetime of the
application, but when the user closes it shouldn't I call all the dispose
methods?
If I use all singleton I have to forget IDisposable and Dispose pattern
implementation and the GC will do all for me?
I don't think it's a clean solution but tell me if I'm wrong!

I've used a mutex to synchronize the main thread (the GUI one) with the one
created for syncing: if syncThread starts before guiThread, the latter will
wait the first one, else guiThread will set doNotStart bool to true and when
the thread can access the critical section it simply returns.
Thank you for your help!
Luigi.
May 20 '07 #5
On May 20, 4:11 pm, "BLUE" <bluewrote:
I am curious, why are you going to have a singleton class that implements
IDispose?

Since I have a class that use an IntPtr and WinCE API calls to detect
insertion into the cradle I have to implement the dispose pattern
(dispose+finali ze) for this singleton class: how to dispose unmanged
resources else?

I suppose the lifetime of all my singleton objects is the lifetime of the
application, but when the user closes it shouldn't I call all the dispose
methods?
If I use all singleton I have to forget IDisposable and Dispose pattern
implementation and the GC will do all for me?
I don't think it's a clean solution but tell me if I'm wrong!

I've used a mutex to synchronize the main thread (the GUI one) with the one
created for syncing: if syncThread starts before guiThread, the latter will
wait the first one, else guiThread will set doNotStart bool to true and when
the thread can access the critical section it simply returns.

Thank you for your help!
Luigi.
Hi,

Nich, I think that nich must implement IDisposable in his singleton
classes.

About the lock in the finalizer:
I would try to avoid this somehow. I think that a time consuming
operations should be avoided either, somehow.

Nich?

May 20 '07 #6
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omschrieb
im Newsbeitrag news:57******** *************** ***********@mic rosoft.com...
Luigi,

The use of lock is discouraged in a finalizer:
Dispose() is not Finalizer.

Dispose(bool) is called by the Finalizer of Form, but always with the value
false for the diposing parameter.

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
May 21 '07 #7
On May 21, 10:37 am, "Christof Nordiek" <c...@nospam.de wrote:
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guar d.caspershouse. comschrieb
im Newsbeitragnews :57************ *************** *******@microso ft.com...Luigi,
The use of lock is discouraged in a finalizer:

Dispose() is not Finalizer.

Dispose(bool) is called by the Finalizer of Form, but always with the value
false for the diposing parameter.

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?

Moty

May 21 '07 #8
"Moty Michaely" <Mo*****@gmail. comschrieb im Newsbeitrag
news:11******** **************@ n15g2000prd.goo glegroups.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
May 21 '07 #9
On May 21, 12:47 pm, "Christof Nordiek" <c...@nospam.de wrote:
"Moty Michaely" <Moty...@gmail. comschrieb im Newsbeitragnews :11************ **********@n15g 2000prd.googleg roups.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))
But either disposing is true or false, I would serialize the class,
wouldn't I?

Moty

May 21 '07 #10

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

Similar topics

11
5313
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...
0
3170
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...
156
5908
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:
2
1964
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...
6
2377
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
8
2012
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 }
1
1551
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...
0
1327
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()
8
4112
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;
0
9680
marktang
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...
1
10173
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,...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
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...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.