|
In c# there's no such thing as a deterministic destructor like in c++. Instead you have a non-deterministic finaliser. If you include a finaliser in your class, when you have finished with the object it will be placed in the finalisation queue, and the finailise method will get called next time garbage collection is run.
This means that your objects may hang around for quite a while, hogging up memory, or holding onto resources, so to help things along we use the dispose pattern which allows us to ensure that objects gets cleared up quickly and finislisation doesn't have to happen. The dispose method should clean up all resourses, and supress finalisation, while the finaliser method should only clear up _unmanaged_ resources. This is becuase the finaliser may get called much later on, so other managaed resources may already have been destoryed, so you mustn't risk referencing them again.
You can then call the dispose method manually when you have finished with the object (or use the "Using" keyword to ensure it gets called)
To read about the dispose pattern check out this site:
http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx
See this page for more about the "Using" keyword
http://msdn2.microsoft.com/en-us/library/yh598w02.aspx
Hope that helps.
Simon
|
|
|
|
|
Thanks alot Simon, that was very useful.
So I conclude that:
1. Finalize method should be used to clear up only unmanaged resources, whereas Dispose method can be used to clearup any object that I create.
Using keyword is very helpful here.
2. We are unable to predict when Finalize would be called and executed by GC, where as Dispose method would be called as soon as the object is out of scope, right?
Regards,
Blumen
|
|
|
|
|
blumenhause wrote: Finalize method should be used to clear up only unmanaged resources
Dispose should be used to clean up resources. A finalizer can be used as a backup when implementing the IDisposable interface, but then it's never really meant to be used as the Dispose method should suppress the finalizer.
A finalizer is not guaranteed to be executed. A background thread executes the finalizers in the finalization queue. If the finalizers left in the queue take too long to run when the application ends, the rest of the objects are just thrown away without finalizing.
blumenhause wrote: where as Dispose method would be called as soon as the object is out of scope
No. The Dispose method is never called automatically. You have to call it explicitly, or use a using block that will call it.
As you always call it explicitly, you always know when it's called.
---
single minded; short sighted; long gone;
|
|
|
|
|
|
You can also take a look at this article: http://www.codeproject.com/useritems/idisposable.asp[^]
Finalizers are expensive to use in that they are difficult to write properly and add an additional memory cost to your finalizable object. Any time a finalizable object is created it is placed in a special queue that must be checked each time the garbage collection cycle runs. This happens even if you never actually use the object.
The best approach is to implement the IDisposable interface and the Dispose pattern properly, and only implement a finalizer when absolutely necessary.
|
|
|
|
|
Hai Everybody,
I am having a tree view in that the nodes are in a hierarchical manner.
The nodes are checkable.
Now when some child nodes are not selected, then I have to make the parent node check box to have the check state "Indeterminate".
But there is no Check State property of the tree node.
Then i tried to convert it as a check box in code, but it is giving the error indicating that it is not possible.
Can any one give me the solution for this?
Best Regards,
M. J. Jaya Chitra
|
|
|
|
|
Try this: Turn the treeviews checkboxes property _false_ and instead create an ImageList and set the treeviews stateimagelist to the imagelist. You can then create three images in the image list (checked/unchecked/intermediate), and use the nodes stateimageindex (or stateimagekey) to set the state image.
Let me know if it works for you, I've never done this before
Simon
|
|
|
|
|
pls, who anyone help me!
I am writting a program to log my PC in a txt file.
when any one start PC it log to txt file and he shutdown it log the txt file.
Thanks
Help you, hepl me
|
|
|
|
|
One hacky type way to stop / slow windows from shutting down, is to show a MessageBox. So, in your apps onClose event (ie. Windows is trying to close your app for shutdown) show a message box while you make your log, then close it.
My current favourite word is: Waffle
Cheese is still good though.
|
|
|
|
|
|
Hi greetings to all [ ];
I am working on a client-Server Project.... (C#.net/SQL)
Need to know ::
1. how can i start the Client(.net) application(s) on a LAN from server application(.net)
2. How to run a particular service(Windows) on the client machine from server (which in turn can communicate with the already running client application)
Thanks n Regards,
Aamu
|
|
|
|
|
hi , Thanks in advance for everyone who reads that!
How can i set a thread so that when i start it the calling thread returns to what it did before and the new thread continues from that point , or any other solution with similar effect ?
for example:
<br />
<br />
public void Some_Event(Object sender , EventArge e)<br />
{<br />
Console.writeline("Im the Old Thread: {0}",Thread.CurrentThread.Name);<br />
<br />
Console.writeline("Im the new Thread: {0}",Thread.CurrentThread.Name);<br />
<br />
SomeForm sf = new SomeForm(((String)sender).username);<br />
<br />
}<br />
<br />
Net
|
|
|
|
|
Google finds this.[^]
You might also want to look at the TreadPool class (Google it). It provides a bit less control over the treads, but in returns handles queuing requets, creating threads etc automatically.
|
|
|
|
|
if i wouldnt read that msdn or google i wouldnt ask here ,
in the msdn they only show examples of
Thred t = new thread(ThreadStart(target));
if you read my question i need something a bit different , dont know if its possible thats why im asking.
Net
|
|
|
|
|
beleive me, a lot of people do NOT consult MSDN or Google before asking, you are the (positive) exception here.
But to answer your question - No, you do not need anything different.
Where you have written "//Some code i dont know how to implement" the code (pseudo code, I am not sure delegate names and signatures match - find them in MSDN) is as follows:
<br />
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMethod));<br />
}
<br />
private void ThreadMethod()
{<br />
}<br />
Depoending on how much control you want with the thread, you can use one of the alternatives (create a Thread object etc) instead of the ThreadPool.
If you REALLY want it in the same method you can replace the "new WaitCallback(ThreadMethod)" part with an anonymous delegate. But I really recommend you try to think of starting threads as follows:
1) The creating thread continues execution in the current method
2) The new thread starts running in a new method/scope.
it will help you later on as this is how the thread methods are designed - and how they are used by most developers.
|
|
|
|
|
The anonymous delegate did the trick ,
Thanks
i did something like that if that's what you mean:
<br />
public void my_multi_threaded_method()<br />
{<br />
Console.WriteLine("calling thread");<br />
<br />
Thread t = new Thread(ThreadStart(delegate() <br />
{<br />
Console.WriteLine("child thread");<br />
});<br />
t.start<br />
}<br />
Net
|
|
|
|
|
plz any ine help me
i need a code for finding whether a file is exisisting in the folder if exist display the file woth some text or not create the file with some text and displaying message that file is created successfully using c#
|
|
|
|
|
you can use System.IO.File.Exists(string path) method to check whether the file exists or not.
good luck
|
|
|
|
|
using System;
using System.IO;
using System.Collections;
public class FileHandling
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// Your code should run here
}
else if(Directory.Exists(path))
{
//Your code should run here
}
else
{
Console.WriteLine("{0} is not a valid file or directory");
}
}
}
Happy Programming
-----
Abhijit
|
|
|
|
|
|
you must first set the WorkerSupportsCancellation Property of BackgroungWorker instance to true.
then you can cancel it
good luck
|
|
|
|
|
Hessam Jalali wrote: you must first set the WorkerSupportsCancellation Property of BackgroungWorker instance to true.
C'mon, I might have screwed up a bit lately but I'm not that stupid
OF COURSE I DID! Yet, it's not working
Thanks anyways Hessam:->
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|
|
really sorry, I didn't mean that ,I make these type of careless mistakes alot myself
can you send some code?
|
|
|
|
|
|
How do you do it guys ..
Clipboard.SetText("C:\SomeFile.txt"); just doesnt seem to work
Smile: A curve that can set a lot of things straight!
(\ /)
(O.o)
(><)
|
|
|
|