dispose

Cleaning objects off a form, Where and When?

好久不见. 提交于 2019-12-05 16:56:57
I have a simple Windows form application. On the form I have a custom class that has it's own Dispose method. So the question is when should I call this? Is the FormClosed event (i.e. Form1_FormClosed) the correct place to do this? Or should I be writing a custom Dispose method for the form? For bonus points: Can a from be reopend once closed? (Obviously if it can then the FormClosed is the wrong way to go!) Thanks. A wee bit of surgery is required. Open the node next to your form in the Solution Explorer window. Double-click the Designer.cs file for the form. Locate the Dispose() method and

Tricky IDisposable Issue

此生再无相见时 提交于 2019-12-05 14:40:11
I'm trying to abstract/encapsulate the following code so all client calls don't need to repeat this code. For example, this is a call, from a view model (MVVM) to a WCF service: using (var channelFactory = new WcfChannelFactory<IPrestoService>(new NetTcpBinding())) { var endpointAddress = ConfigurationManager.AppSettings["prestoServiceAddress"]; IPrestoService prestoService = channelFactory.CreateChannel(new EndpointAddress(endpointAddress)); this.Applications = new ObservableCollection<Application>(prestoService.GetAllApplications().ToList()); } My original attempt at refactoring was to do

How do I know if UdpClient has been closed/disposed?

混江龙づ霸主 提交于 2019-12-05 13:13:10
I am receiving data from UdpClient via the usual async callback: private void OnUdpData(IAsyncResult result) { byte[] data = _udpReceive.EndReceive(result, ref _receiveEndPoint); //Snip doing stuff with data _udpReceive.BeginReceive(OnUdpData, null); } When I Close() the UdpClient in the main thread, the callback fires as I would expect, but at this point _udpReceive is already disposed and I get an ObjectDisposedException when I try and call EndReceive() . I was expecting to just get an empty buffer. What is the correct way to handle this? Is there some member of UdpClient I can check before

Async Disposable.Create

半城伤御伤魂 提交于 2019-12-05 10:33:03
Disposable.Create require an Action as parameter. The Action is run when the Rx subscription is being disposed. When disposing a Rx subscription I’d like to run some asynchronous clean up code, however using async () => with Action is identical to async void , which I’d like to avoid. For more details on why I want to avoid this, see here . Is it possible to create something like a Disposable.AsyncCreate , which accepts Func<Task> rather than Action . If so how should I use it as part of a CompositeDisposable ? Or are there other patterns for dealing with asynchronous Disposal? You could do

Is there a way to dynamically create and dispose of Webbrowser control?

血红的双手。 提交于 2019-12-05 05:16:37
问题 I have this App that uses the Webbrowser control to do automated browsing. I need to come up with a way to automatically close the browser (dispose), then create another instance that actually works. Here's some of the code that I have so far. this.webBrowser2 = new System.Windows.Forms.WebBrowser(); this.webBrowser2.Dock = System.Windows.Forms.DockStyle.Bottom; this.webBrowser2.Location = new System.Drawing.Point(0, 34); this.webBrowser2.MinimumSize = new System.Drawing.Size(20, 20); this

How to dispose a Writeable Bitmap? (WPF)

独自空忆成欢 提交于 2019-12-05 05:12:21
Some time ago i posted a question related to a WriteableBitmap memory leak, and though I received wonderful tips related to the problem, I still think there is a serious bug / (mistake made by me) / (Confusion) / (some other thing) here. So, here is my problem again: Suppose we have a WPF application with an Image and a button. The image's source is a really big bitmap (3600 * 4800 px), when it's shown at runtime the applicaton consumes ~90 MB. Now suppose i wish to instantiate a WriteableBitmap from the source of the image (the really big Image), when this happens the applications consumes

C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader

北慕城南 提交于 2019-12-05 03:58:20
I have lots of code like this: FileStream fs = File.Open(@"C:\Temp\SNB-RSS.xml", FileMode.Open); using (XmlTextReader reader = new XmlTextReader(fs)) { /* Some other code */ } This gives me the following Code Analysis warning: CA2000 : Microsoft.Reliability : In method 'SF_Tester.Run()', object 'fs' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'fs' before all references to it are out of scope. If I follow the suggestion and I put the File.Open in a using statement, I get this: CA2202 : Microsoft.Usage : Object 'fs' can be disposed more than once in

Form.ShowDialog() and dispose

最后都变了- 提交于 2019-12-05 02:02:51
If I have a method like this: public void Show() { Form1 f = new Form1(); f.ShowDialog(); } Do I still need to call dispose on the form even though it will go out of scope, which will be eligible for garbage collection. From some testing, calling this Show() multiple times .. at some point it seems like the GC collects it since I can see the memory spiking then it goes down at some point in time. From MSDN it seems to say you MUST call dispose when the form is not needed anymore. In your specific example, no, it's unlikely that it would be particularly useful. Forms do not hold onto a

Explain the mysterious world of IoC and automatic Dispose

有些话、适合烂在心里 提交于 2019-12-04 19:18:09
I'm ASP.NET MVC newbye and I'm learning and experimenting Enterprise design patterns: very interesting and helpful things indeed! But I keep missing something about the concept of disposing resources. To be more specific want to focus on the mechanism of controllerFactory which injects in the controller constructor an implementation of IFuncyRepository or IFuncyService or anyother kind of "resource" to be used in the controller (In my case I'm using StructureMap as IoC). My question is WHERE / IF / HOW these injected resources get Disposed. The book I'm following as guideline is ASP.NET Design

Dispose question

旧街凉风 提交于 2019-12-04 17:54:10
问题 I have a number of classes which have private member variables that implement IDisposable (timers, brushes, etc). Do I need to do anything to ensure these variables are cleaned up properly by the .NET Framework? The literature I've come across is referring to "managed resources" vs. "unmanaged resources". These terms are confusing to me because you can have a managed class which implements functionality using unmanaged resources. Is that considered an "unmanaged resource" or "managed resource