dispose

What's the point of overriding Dispose(bool disposing) in .NET?

只谈情不闲聊 提交于 2019-12-03 01:38:01
问题 If I write a class in C# that implements IDisposable, why isn't is sufficient for me to simply implement public void Dispose(){ ... } to handle freeing any unmanaged resources? Is protected virtual void Dispose(bool disposing){ ... } always necessary, sometimes necessary, or something else altogether? 回答1: It's not strictly necessary. It is part of the recommended Disposable pattern. If you haven't read the Framework Design Guidelines section on this (9.3 in the first edition, don't have the

How to dispose resources with dependency injection

此生再无相见时 提交于 2019-12-03 01:14:47
I'm using StructureMap to resolve references to my repository class. My repository interface implements IDisposable, e.g. public interface IMyRepository : IDisposable { SomeClass GetById(int id); } An implementation of the interface using Entity Framework: public MyRepository : IMyRepository { private MyDbContext _dbContext; public MyDbContext() { _dbContext = new MyDbContext(); } public SomeClass GetById(int id) { var query = from x in _dbContext where x.Id = id select x; return x.FirstOrDefault(); } public void Dispose() { _dbContext.Dispose(); } } Anyway as mentioned I'm using StructureMap

C# how to implement Dispose method

旧巷老猫 提交于 2019-12-02 15:33:24
I need some advice on the implementation of the Dispose method. In our application the user designs their own UI. I have a preview window that shows what the UI is going to look like. All object drawn in this UI ultimately derive from a common base class ScreenObject. My preview manager contain a single object reference to a ScreenGrid which is the grid object for the entire preview area. Question #1 Some of my derived screen classes hold onto unmanaged resources, such as a database connection, bitmap image and a WebBrowser control. These classes need to dispose of these objects. I created a

What's the point of overriding Dispose(bool disposing) in .NET?

隐身守侯 提交于 2019-12-02 15:07:24
If I write a class in C# that implements IDisposable, why isn't is sufficient for me to simply implement public void Dispose(){ ... } to handle freeing any unmanaged resources? Is protected virtual void Dispose(bool disposing){ ... } always necessary, sometimes necessary, or something else altogether? It's not strictly necessary. It is part of the recommended Disposable pattern. If you haven't read the Framework Design Guidelines section on this (9.3 in the first edition, don't have the second edition handy sorry) then you should. Try this link . It's useful for distinguishing between

Self-closing StreamWriter singleton

落花浮王杯 提交于 2019-12-02 06:54:30
问题 I tried to further narrow down the problem in Flush StreamWriter at the end of its lifetime implementing a singleton-like self-closing StreamWriter : class Foo : System.IO.StreamWriter { private static readonly Foo instance = new Foo( "D:/tmp/test" ); private Foo( string path ) : base( path ) { } ~Foo() { this.Close(); } public static Foo Instance { get { return instance; } } } The intended effect is that in a sample program like class Program { private static void Main( string[] args ) { Foo

Exception in lazy loading (Entity Framework)

▼魔方 西西 提交于 2019-12-02 06:21:42
问题 I use Entity Framework in my project. The issue is well known but supposed solutions (eg. this and this) doesn't work for me. /// <summary> /// Returns complete list of lecturers from DB. /// </summary> public IEnumerable<Lecturer> GetAllLecturers() { IList<Lecturer> query; using (var dbb = new AcademicTimetableDbContext()) { query = (from b in dbb.Lecturers select b).ToList(); } Debug.WriteLine(query[0].AcademicDegree); // Exception (***) return query; } Exception (***): The ObjectContext

java swing program not closing after dispose is called on last window

风流意气都作罢 提交于 2019-12-02 04:26:59
Preface: This is the first real swing program I have done. I have a swing program, where one JButton is supposed to exit the program. That button triggers this.dispose();. When i click this JButton, it does make the window completely go away, but looking at the debugger, the program itself is still running. My main method only consists of: public static void main (String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new StartupGui().setVisible(true); } }); } My exit button looks like action button looks like: private void exitButtonActionPerformed(java.awt

Dispose of a pictureBox image without losing the pictureBox

╄→гoц情女王★ 提交于 2019-12-02 04:23:56
问题 I am writing a program that will play a slideshow (among other things). The slideshow is controlled by a backgroundWorker, and is set to a while(true) loop so it will constantly play images. My problem is I'm not sure how to dispose of the old images so they don't take up memory (after a while the program throws an "Out of memory exception). If I call horPicBox.Image.Dispose() then it won't let me use the pictureBox at all after that. Is there a way to release the old image from memory?? If I

Self-closing StreamWriter singleton

那年仲夏 提交于 2019-12-02 04:08:29
I tried to further narrow down the problem in Flush StreamWriter at the end of its lifetime implementing a singleton-like self-closing StreamWriter : class Foo : System.IO.StreamWriter { private static readonly Foo instance = new Foo( "D:/tmp/test" ); private Foo( string path ) : base( path ) { } ~Foo() { this.Close(); } public static Foo Instance { get { return instance; } } } The intended effect is that in a sample program like class Program { private static void Main( string[] args ) { Foo.Instance.Write( "asdf\n" ); } } the garbage collector causes the instance of Foo to be closed. This

Java dialog does not dispose

廉价感情. 提交于 2019-12-02 02:41:57
问题 Java noob here. My Swing class that extends JDialog does not dispose when the user presses the Windows Close button - java.exe stays in memory. I've stripped the code right down to this shell, I still get this behaviour. I took a look at other samples, such as at Basic Java Swing, how to exit and dispose of your application/JFrame When I commented out the two System.exit(0) lines in that sample code, the class in that sample still disposed correctly. What am I missing to make my class dispose