dispose

SqlDataReader: In this scenario, will the reader get closed?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 03:48:15
问题 I am cleaning up the DataReaders in an old .NET 1.1 project that I inherited. The previous developer coded the data-access-layer in such a way that most of the DAL methods returned SqlDataReaders (thus leaving it up to the caller to properly call the .Close() or .Dispose() methods). I have come across a situation, though, where a caller is not catching the returned SqlDataReader (and therefore is not disposing of it properly). See the code below: Data Access Method: Public Shared Function

Not Using Dispose or Using() in Entity Framework

北战南征 提交于 2019-12-24 01:57:20
问题 I was writing a web app and learning entity framework along the way. I am curios if I have done something wrong though. I haven't been using dispose or using statements when querying. Example of a Repository of mine: public User GetUserById(int sessionId) { var user = (from u in db.Users where u.Id == sessionId select u).SingleOrDefault(); return user; } My User table I selected from would have foreign keys which would be associated with other tables. The cool thing, or so I thought, about

Use of NativeWindow for ComboBox causes exception in Dispose-method

醉酒当歌 提交于 2019-12-24 00:20:09
问题 In C# Windows.Forms I want to intercept the paste-windowmessage for a combobox. As this doesn't work by overriding the WndProc-method of the combobox, because I would need to override the WndProc of the textbox inside the combobox, I decided to create a custom class of type NativeWindow which overrides the WndProc. I assign the handle and release it, when the combobox-handle gets destroyed. But when Dispose for the combobox is called the problem is that I get an InvalidOperationException

Disposing of arguments for an iterator block

天大地大妈咪最大 提交于 2019-12-23 18:31:08
问题 Allright, here it goes a good piece of bad code: public class Log : CachingProxyList<Event> { public static Log FromFile(String fullPath) { using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { return new Log(sr); } } } public Log(StreamReader stream) : base(Parser.Parse(Parser.Tokenize(stream))) { /* Here goes some "magic", the whole reason for this * class to exist, but not really relevant to the issue */ } } And

When can't you use SafeHandle over Finalizer/IDisposable?

安稳与你 提交于 2019-12-23 13:06:47
问题 When seeing about the whole finalizer/IDisposable issue, it is usual to see that, at the end, after all the long description, there will be something to the meaning of "LOL what I said was actually useless, you should use SafeHandle instead bye!" So I am wondering in what case does SafeHandle not fit, such that you have to resort to the finalizer/IDisposable old way? 回答1: Clearly, when the unmanaged resource you are wrapping is not acquired through a handle. Which is rare but not unheard of.

SqlCommand-SqlConnection Using Disposing issue

一笑奈何 提交于 2019-12-23 12:30:47
问题 According to MSDN If the IDisposable resource of the nested inner using statement contains the resource of the outer using statement, the Dispose method of the nested resource releases the contained resource. MSDN (http://msdn.microsoft.com/en-us/library/ms182334.aspx) => Example Nested using statements (Using in Visual Basic) can cause violations of the CA2202 warning. If the IDisposable resource of the nested inner using statement contains the resource of the outer using statement, the

Do I need to implement a dispose or finalize in my objects?

天大地大妈咪最大 提交于 2019-12-23 12:10:05
问题 For too long I let the garbage collector do its magic, removing all responsibilities from my self. Sadly it never turned into an issue... So I never gave a second thought to the subject. Now when I think about it I don't really understand what the "dispose" function really does and how and when it should be implemented. The same question for finalize... And a last question... I have a class pictureManipulation : when I need to save/resize/change format ... I start a new instance of that class

Will using a SPListItemCollection returned from a function reopen the SPWeb?

拈花ヽ惹草 提交于 2019-12-23 10:20:29
问题 After reading Stefan Gossner's post about disposing objects and this question about Cross method dispose patterns, I found that I was guilty of accidentally reopening some SPWebs. I know in Stefan Gossner's post he mentions you should dispose of an SPWeb after you are finished with any child object. However, the microsoft documentation mentions Caching the SPListItemCollection object. Is the following code correct? Would the returned SPListItemCollection reopen an SPWeb object? Is there any

Can I dispose a Threading.Timer in its callback?

心不动则不痛 提交于 2019-12-23 09:31:54
问题 I'm using Threading.Timer , like: new Timer(new TimerCallback(y=> { try { Save(Read(DateTime.Now)); // here i want to dispose this timer } catch { } }),null,100000,10000); How can I dispose this timer inside of a callback. or workaround? Update: Let me explain the situation. I want to try to call the method "Save", while it throws an exception. If it works, I need to stop the timer. 回答1: Try this: Timer timer = null; timer = new Timer(new TimerCallback(y => { try { Save(Read(DateTime.Now)); /

Use of Garbage Collection?

假如想象 提交于 2019-12-23 07:51:14
问题 I want to know what action is performed when we call Dispose() method. Is Object frees all resources quickly on Dispose() call or Dispose() marks the Object is ready for garbage collection. And What happened when we set Object reference to NULL. Actually I have Windows form application in .NET 2.0. And I want to call garbage collector after certain time is passed(For Example after 5 mins) to collect all unreferenced Object. 回答1: There is nothing magic about the Dispose method, it's just like