dispose

Will a using clause close this stream?

落爺英雄遲暮 提交于 2019-11-28 06:56:24
问题 I've apparently worked myself into a bad coding habit. Here is an example of the code I've been writing: using(StreamReader sr = new StreamReader(File.Open("somefile.txt", FileMode.Open))) { //read file } File.Move("somefile.txt", "somefile.bak"); //can't move, get exception that I the file is open I thought that because the using clause explicitly called Close() and Dispose() on the StreamReader that the FileStream would be closed as well. The only way I could fix the problem I was having

Do I need to dispose a web service reference in ASP.NET?

守給你的承諾、 提交于 2019-11-28 05:33:43
Does the garbage collector clean up web service references or do I need to call dispose on the service reference after I'm finished calling whatever method I call? Dan Herbert Instead of worrying about disposing your web services, you could keep only a single instance of each web service, using a singleton pattern . Web services are stateless, so they can safely be shared between connections and threads on a web server. Here is an example of a Web Service class you can use to hold references to your web service instances. This singleton is lazy and thread-safe. It is advised that if you make

.NET Webbrowser Control and Dispose()

妖精的绣舞 提交于 2019-11-28 05:16:54
问题 I know this is a hot discussed topic with many questions and answers but I still do not find the solution for the following problem: I have a multi-tab application. On every tab is a Webbrowser control. As the webbrowsers take more an more memory for every new tab and they do not free this memory on tab-close, I decided to make a Webbrowser.Dispose() in the tab close event handler. This helped me concerning the memory leak. all the used RAM is now free after closing. But this caused a new

Dispose() for cleaning up managed resources?

試著忘記壹切 提交于 2019-11-28 05:01:52
In this answer I found, Cleanup the unmanaged resources in the Finalize method and the managed ones in the Dispose method, when the Dispose/Finalize pattern has been used in your code. And later I found this nice article about finalize and dispose and got a clear idea about them. The article has the following code( Page 3 ), to explain the concepts: class Test : IDisposable { private bool isDisposed = false; ~Test() { Dispose(false); } protected void Dispose(bool disposing) { if (disposing) { // Code to dispose the managed resources of the class } // Code to dispose the un-managed resources of

Dispose vs Dispose(bool)

萝らか妹 提交于 2019-11-28 04:21:38
I am confused about dispose. I am trying to get my code disposing resources correctly. So I have been setting up my classes as IDisposable (with a Dispose method) them making sure that the Dispose method gets called. But now FXCop is telling me lots of stuff about Disposing = false and calling Dispose(false). I don't see a Dispose method that takes a bool. Do I need to make one? If so, why? Why not just have a method that gets called when it is disposing? I saw some code here: http://msdn.microsoft.com/en-us/library/ms244737.aspx that shows how to make a Disposing method that takes a bool. It

MemoryStream must be explicitely be disposed?

淺唱寂寞╮ 提交于 2019-11-28 03:57:20
问题 As MemoryStream is an unmanaged resource does it always have to be disposed? Given: 1) A method is invoked. 2) A MemoryStream object is created (MemoryStream ms = new MemoryStream();). 3) An exception occurs and is caught from the invoking classes. The reference on the MemoryStream object is therefore lost. Does this scenario need a try/finally-block (or using-statement)? 回答1: In general, all disposable objects must always be disposed. However, MemoryStream doesn't actually need to be

Does SqlTransaction need to have Dispose called?

让人想犯罪 __ 提交于 2019-11-28 03:48:39
问题 Do I need to call dispose in the finally block for SqlTransaction? Pretend the developer didnt use USING anywhere, and just try/catch. SqlTransaction sqlTrans = con.BeginTransaction(); try { //Do Work sqlTrans.Commit() } catch (Exception ex) { sqlTrans.Rollback(); } finally { sqlTrans.Dispose(); con.Dispose(); } 回答1: Do I need to use try-finally or the using -statement to dispose the SqlTransaction ? It does not hurt to have it. This is true for every class implementing IDisposable, otherwise

How do you prevent IDisposable from spreading to all your classes?

十年热恋 提交于 2019-11-28 02:59:59
Start with these simple classes... Let's say I have a simple set of classes like this: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new Shoelace(); } class Shoelace { bool tied = false; } A Bus has a Driver , the Driver has two Shoe s, each Shoe has a Shoelace . All very silly. Add an IDisposable object to Shoelace Later I decide that some operation on the Shoelace could be multi-threaded, so I add an EventWaitHandle for the threads to communicate with. So Shoelace now looks like this: class Shoelace {

Dispose JFrame from another class

偶尔善良 提交于 2019-11-28 02:27:22
How can I dispose JFrame from another class? My code is listed below. I use Netbeans generate form for generate window. And I want dispose using another class(name is needDispose). public class needDispose { /** *Call for dispose frame */ public static void disposeMyFrame(){ myEasyFrame.getMainFrame.dispose(); // DOESNT WORK } } NETBEANS GENERATE (important method is getMainFrame() ) import javax.swing.JFrame; public class myEasyFrame extends javax.swing.JFrame { /* * Get frame */ public final JFrame getMainFrame() { return this; } /** * Creates new form myEasyFrame */ public myEasyFrame() {

Datatable.Dispose() will make it remove from memory?

不羁的心 提交于 2019-11-28 01:07:15
I have researching through very simple code and get stuck on seeing the dispose() result of datatable Following is the code DataTable dt= new Datatable(); SqlCommand Cmd = new SqlCommand("sp_getData",SqlCon); SqlCommand.CommandType= CommandType.StroedProcedure; SqlCon.Open(); sqlDataReader dr= cmd.ExecuteReader(); dt.Load(dr); SqlCon.Close(); grdView.DataSource =dt; dt.Dispose() // Here I dispose the table as it is no use for me & wanna memory free from this But after disposing off the datatable i still found that it is still showing RowCount = 10k. Does Dispose() method does not free up the