dispose

Can a CryptoStream be returned and still have everything dispose correctly?

我与影子孤独终老i 提交于 2019-12-01 04:01:40
If I have a CryptoStream that I want to pass back to the user, the naïve approach would be public Stream GetDecryptedFileStream(string inputFile, byte[] key, byte[] iv) { var fsCrypt = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read); var rmCrypto = new RijndaelManaged(); var transform = rmCrypto.CreateDecryptor(key, iv); var cs = new CryptoStream(fsCrypt, transform, CryptoStreamMode.Read); return cs; } I know that when I dispose the CryptoStream the underlying FileStream will also be disposed . The issue I am running in to is what do I do with rmCrypto and transform ?

Structuremap Disposing of DataContext object

自闭症网瘾萝莉.ら 提交于 2019-12-01 03:59:53
I wanted to be sure if structuremap will dispose my DataContext after per request ends. Here is my setup ForRequestedType<MyDataContext>().TheDefault.Is.OfConcreteType<MyDataContext>(); SelectConstructor<MyDataContext>(() => new MyDataContext()); Will structuremap auto dispose my datacontext or do i need to call Dispose manually?? No it will not Dispose it automatically, unless you use nested containers and Dispose the container holding the context instance. It's up to the creator of the context to Dispose it. The creator would usually be the part of your code calling ObjectContext.GetInstance

Dispose SmtpClient in SendComplete?

删除回忆录丶 提交于 2019-12-01 03:36:48
When I use SmtpClient's SendAsync to send email, how do I dispose the smtpclient instance correctly? Let's say: MailMessage mail = new System.Net.Mail.MailMessage() { Body = MailBody.ToString(), IsBodyHtml = true, From = new MailAddress(FromEmail, FromEmailTitle), Subject = MailSubject }; mail.To.Add(new MailAddress(i.Email, "")); SmtpClient sc = new SmtpClient(SmtpServerAddress); //Add SendAsyncCallback to SendCompleted sc.SendCompleted += new SendCompletedEventHandler(SendAsyncCallback); //using SmtpClient to make async send (Should I pass sc or mail into SendAsyncCallback?) sc.SendAsync

Problems solving “Cannot access disposed object.” exception

不问归期 提交于 2019-12-01 02:55:49
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 code UpdateDistanceLabel(distance); UpdateSpeedLabel(v); } previousLocation = Cursor.Position; }

Does calling Clear disposes the items also?

半城伤御伤魂 提交于 2019-12-01 02:12:55
Many times there is a clear method, that removes all the items from the collections, are these items disposed also. Like, toolStripMenuItem.DropDownItems.Clear(); is sufficient, or should I have to call like that: foreach (ToolStripItem item in toolStripMenuItem.DropDownItems) { toolStripMenuItem.DropDownItems.Remove(item); item.Dispose(); } Edit: Well ToolStripItem is an example not a question, for those who says Clear is enough I found another example, TabControl has also item collection and clear method. But TabControls can have complex controls (at least I have), which needs to be

Make using statement usable for multiple disposable objects

て烟熏妆下的殇ゞ 提交于 2019-12-01 02:01:41
I have a bunch of text files in a folder, and all of them should have identical headers. In other words the first 100 lines of all files should be identical. So I wrote a function to check this condition: private static bool CheckHeaders(string folderPath, int headersCount) { var enumerators = Directory.EnumerateFiles(folderPath) .Select(f => File.ReadLines(f).GetEnumerator()) .ToArray(); //using (enumerators) //{ for (int i = 0; i < headersCount; i++) { foreach (var e in enumerators) { if (!e.MoveNext()) return false; } var values = enumerators.Select(e => e.Current); if (values.Distinct()

Is this IDisposable implementation correct?

[亡魂溺海] 提交于 2019-12-01 01:07:57
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: public abstract class Disposable : IDisposable { private bool _isDisposed; private readonly object

Finalizers with Dispose() in C#

会有一股神秘感。 提交于 2019-11-30 22:16:29
See the code sample from MSDN: ( http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=VS.100).aspx ) // Design pattern for a base class. public class Base: IDisposable { private bool disposed = false; //Implement IDisposable. public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // Free other state (managed objects). } // Free your own state (unmanaged objects). // Set large fields to null. disposed = true; } } // Use C# destructor syntax for finalization code. ~Base() { // Simply call Dispose(false)

MVC 3 - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

删除回忆录丶 提交于 2019-11-30 21:33:00
I'm very new to C# and MVC in general and I've been creating my own little blog site as a test project. Although most things are working up to this point, I have had problems selecting multiple columns from LINQ queries. It was only after stumbling on a question on SO that I realised I could use the generated entities classes as strongly-typed models to handle this. I've needed this as I've been creating a database layer (which is also something I've not used before) and trying to pass anonymous types through that layer didn't work. I understand why that was the case, so I'm satisfied that I'm

(.net) CriticalFinalizerObject - What does it really do?

一笑奈何 提交于 2019-11-30 20:56:02
My understanding about this class is that you should use it when you want to be sure that the Finalizer(destructor) or the class is called, but from a couple of tests I did, it doesn't seem to be true. If it does not make sure that the dispose method is called, is there any other way of doing it? For example, if i want to make sure that some code is run to end my object, even if I close my program via Task Manager or something? Chris Brumme has explained this topic in a way I'm not sure could ever be topped. :) from a couple of tests I did, it doesn't seem to be true. Finalizers in .Net are