idisposable

Infinite state machine with an IDisposable

半腔热情 提交于 2019-12-12 11:20:46
问题 Lets say I have an infinite state machine to generate random md5 hashes: public static IEnumerable<string> GetHashes() { using (var hash = System.Security.Cryptography.MD5.Create()) { while (true) yield return hash.ComputeHash(Guid.NewGuid().ToByteArray()); } } In the above example I use an using statement. Will the .Dispose() method ever be called? CQ, will the unmanaged resources ever be freed? For example, if I use the machine as follows: public static void Test() { int counter = 0; var

Creating an IDisposable class in c# which cleans up an SqlConnection when finished with

拜拜、爱过 提交于 2019-12-12 11:19:43
问题 In an answer to a previous question somebody recommended: have the SqlConnection a member variable of your class, but make the class IDisposable and dispose of the SqlConnection when the class is disposed I have put together an implementation of this suggestion (below) but wanted to check that this implementation is correct (obviously it doesn't currently do anything except open the connection but the idea is that there would be methods in there which would use the connection and which would

What is the resulting behavior when an IDisposable is passed into a parent IDisposable

本小妞迷上赌 提交于 2019-12-12 10:46:49
问题 Yesterday, after running Visual Studio code analysis on our codebase, the following code was highlighted as an issue: using (var stringReader = new StringReader(someString)) { using (var reader = XmlReader.Create(stringReader)) { // Code } } The warning that was returned was Warning CA2202 Object 'stringReader' can be disposed more than once in method '(method name)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object. After

CA2213 warning when using ?. (null-conditional Operator) to call Dispose

时间秒杀一切 提交于 2019-12-12 09:34:24
问题 I'm implementing IDisposable , and in my Dispose() method when calling Dispose() on other managed resources I'm using the ?. operator like so: public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if(disposing) { _ChangeLock?.Dispose(); } } I'm still getting the following code analysis error: CA2213: 'MyClass' contains field 'MyClass._ChangeLock' that is of IDisposable type: 'ReaderWriterLockSlim'. Change the Dispose method on

How to implement IDisposable in Entity Framework?

牧云@^-^@ 提交于 2019-12-12 09:01:25
问题 I am having my entity framework context in a separate EL Layer, which stands for Entity Layer and then I move to DAL, then BL and my user inteface aspx.cs code page. I am confused as such how to use IDisposable in the same. What I am doing till now, supopose in my DAL I have context of my entities. namespace abc { public class Action: IDisposable { Entities context = new Entities(); // all the methods public void Dispose() { context.Dispose(); } } } Is it the correct way of doing so? I am

using statement on IDisposable object - delay of calling Dispose method

我是研究僧i 提交于 2019-12-12 08:47:43
问题 As describe this article, about usage of using on IDisposable objects, it says one interesting words: ...using block, the Dispose method is automatically called sometime after the block ends. (It may not be immediate; it depends on the CLR.) Interesting here is " It may not be immediate; it depends on the CLR ". Can anyone provide more details on this? Because we have some strange situations where it seems that on code using(new MyDisposable()) {...}, after end of block } it does NOT

Making Windows Forms controls read-only and IDisposable

落花浮王杯 提交于 2019-12-12 04:38:39
问题 I want to make Windows Forms controls readonly and IDisposable. Is this is a good thing or a bad thing and what do I have to watch out for when calling Dispose? I have a Tab (PageTab) that I extend and insert a Panel which has a listview and another Toolbar control into. These tabs then get inserted into a tab control (all native .NET Windows Forms controls). When the user closes one of these tabs I call the Dispose method (which follows the MSDN way of implementing IDisposable). Is it wise

C# On Quit WebPage Delete Files and Folders on Server with no user action

梦想与她 提交于 2019-12-12 01:29:58
问题 I have some problems to delete temporary folder and files on my server when users not finish some action in webpages and quit to other webpages. Initialy at Page Load folders are created to allow the user to load files.I have tried implementing destruction during Idisposable without success. Could someone point the best method to delete folders and files when user quit the page with no action or cancel button. 回答1: You can investigate the OnUnload event of the page but, honestly, your best

Why does WPF not use IDisposable, and what are the ramifications?

谁都会走 提交于 2019-12-11 19:52:42
问题 So, I've never done a sizable WPF project before now, but this one has me wondering; What's up with WPF classes not implementing IDisposable ? By comparison, all of the UI elements in Windows Forms implement IDisposable , to assure they get rid of the underlying handles and such. I think the same Windows objects are under the covers there, and those resources have to be released; so, how is WPF doing that? Is there anything I need to be doing with my WPF Window objects beyond Close() ing them

Is it necessary to Dispose() when using a custom ServiceHostFactory?

﹥>﹥吖頭↗ 提交于 2019-12-11 11:17:01
问题 Is it necessary to Dispose() when using a custom ServiceHostFactory? In my WCF .svc file I have defined a custom Factory as: <%@ ServiceHost Factory="Service.ServiceHostFactory" %> It appears that a Dispose() is not being called since the overridden CreateServiceHost() method is not being called at each execution of the application calling the service. (Also, among other things, logging is not being performed after each call and the trace.xml file I generated says that it is in use by another