dispose

C#: Dispose() a Bitmap object after call Bitmap.save()?

给你一囗甜甜゛ 提交于 2020-01-01 12:09:42
问题 I have this: Bitmap bmp = new Bitmap(image); //image processing bmp.Save(path + fileName); and I want to know if I need to call bmp.Dispose() after this code. Thanks in advance. 回答1: I would use using block and Path.Combine using(var bmp = new Bitmap(image)) { // image processing bmp.Save(Path.Combine(path ,fileName)); } 回答2: Yes. Better yet, you could wrap your bmp in a using block, this will take care of the disposal for you using(var bmp = new Bitmap(image)) { bmp.Save(...); } Save's sole

How do I correctly manage the disposing of a DataContext?

删除回忆录丶 提交于 2020-01-01 05:12:45
问题 I have a web service that is quite heavy on database access. It works fine in test, but as soon as I put it in production and ramp up the load it starts churning out errors that are raised when something calls a method in the DataContext. The error is normally one of these: Object reference not set to an instance of an object Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'. but not always. Any single web service requests can result as many as 10 or 15

SqlCommand (Using Statement / Disposing issue)

可紊 提交于 2020-01-01 04:45:10
问题 Take the following example... Using cn As New SqlConnection(ConnectionString) Try Dim cmd As SqlCommand = New SqlCommand With cmd .Connection = cn .Connection.Open() .CommandText = "dbo.GetCustomerByID" .CommandType = CommandType.StoredProcedure .Parameters.Add("@CustomerID", SqlDbType.Int, 4) .Parameters("@CustomerID").Value = CustomerID End With da = New SqlDataAdapter(cmd) da.Fill(ds, "Customer") Catch ex As Exception End Try End Using From my research today is sounds as though this is

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

本秂侑毒 提交于 2020-01-01 02:39:31
问题 I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHorse { void Eat(IApple apple); // is supposed to call apple.Consume() } interface IHorseKeeper { void FeedHorse(); // is supposed to call horse.Eat(apple) // where

What are the rules for when dispose() is required?

ε祈祈猫儿з 提交于 2019-12-30 09:57:10
问题 Although I have been coding for some time, I'm really just barely into what I would call an intermediate level coder. So I understand the principle of dispose(), which is to release memory reserved for variables and/or resources. I have also found sometimes using EF I have to dispose() in order for other operations to work properly. What I don't understand is just exactly what requires a release, when to employ dispose(). For example, we don't dispose variables like string, integer or

Problems solving “Cannot access disposed object.” exception

拟墨画扇 提交于 2019-12-30 08:07:23
问题 In my current project there is a Form class which looks like this: public partial class FormMain : Form { System.Timers.Timer timer; Point previousLocation; double distance; public FormMain() { InitializeComponent(); distance = 0; timer = new System.Timers.Timer(50); timer.AutoReset = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Start(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (previousLocation != null) { // some

Is this IDisposable implementation correct?

拈花ヽ惹草 提交于 2019-12-30 07:33:27
问题 I can never remember all the rules for implementing the IDisposable interface, so I tried to come up with a base class that takes care of all of this and makes IDisposable easy to implement. I just wanted to hear your opinion if this implementation is ok as it is or whether you see something I could improve. The user of this base class is supposed to derive from it and then implement the two abstract methods ReleaseManagedResources() and ReleaseUnmanagedResources() . So here is the code:

How can I dispose System.Xml.XmlWriter in PowerShell

隐身守侯 提交于 2019-12-30 04:27:11
问题 I am trying to dispose XmlWriter object: try { [System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml') } finally { $writer.Dispose() } Error: Method invocation failed because [System.Xml.XmlWellFormedWriter] doesn't contain a method named 'Dispose'. On the other side: $writer -is [IDisposable] # True What should I do? 回答1: Dispose is protected on System.Xml.XmlWriter . You should use Close instead. $writer.Close 回答2: Here is an alternative approach: (get-interface $obj

unable to delete image after opening it in vb.net app

二次信任 提交于 2019-12-29 07:01:08
问题 I have this code: Dim xx as image xx = image.fromfile(Fileloc) picturebox.image = xx And i can't delete the file even though I've loaded it into a picture box. If I add this line: xx.dispose the picture box becomes a big red X. I only want to delete the images when my application is closing (they are temp files). So shall I just dispose them before I delete them? 回答1: Don't use Image.FromFile , it keeps the file open. From MSDN : The file remains locked until the Image is disposed. Do that

unable to delete image after opening it in vb.net app

萝らか妹 提交于 2019-12-29 07:01:02
问题 I have this code: Dim xx as image xx = image.fromfile(Fileloc) picturebox.image = xx And i can't delete the file even though I've loaded it into a picture box. If I add this line: xx.dispose the picture box becomes a big red X. I only want to delete the images when my application is closing (they are temp files). So shall I just dispose them before I delete them? 回答1: Don't use Image.FromFile , it keeps the file open. From MSDN : The file remains locked until the Image is disposed. Do that