Do I need to dispose of a Task?

While the normal rule of thumb is to always call Dispose() on all IDisposable implementations, Task and Task<T> are often one case where it’s better to let the finalizer take care of this. The reason Task implements IDisposable is mainly due to an internal WaitHandle. This is required to allow task continuations to work properly, … Read more

Adding / Removing components on the fly

As suggested by Tim, quoting @tbosch’s comment Angular reuses previously created DOM elements by default So to avoid this behavior, taken from the comment as well, you can use APP_VIEW_POOL_CAPACITY and assign it 0 as value. bootstrap(MyApp, [provide(APP_VIEW_POOL_CAPACITY, {useValue: 0})]) Update Note that since beta.1 APP_VIEW_POOL_CAPACITY was removed by #5993 and the DOM is being … Read more

Datatable.Dispose() will make it remove from memory?

DataSet and DataTable don’t actually have any unmanaged resources, so Dispose() doesn’t actually do much. The Dispose() methods in DataSet and DataTable exists ONLY because of side effect of inheritance – in other words, it doesn’t actually do anything useful in the finalization. It turns out that DataSets, DataViews, DataTables suppress finalization in their constructorsc … Read more

Who Disposes of an IDisposable public property?

There is no single answer, it depends on your scenario, and the key point is ownership of the disposable resource represented by the property, as Jon Skeet points out. It’s sometimes helpful to look at examples from the .NET Framework. Here are three examples that behave differently: Container always disposes. System.IO.StreamReader exposes a disposable property … Read more

Do I need to Dispose a SemaphoreSlim?

If you access the AvailableWaitHandle property, then Yes, you must call Dispose() to cleanup unmanaged resources. If you do not access AvailableWaitHandle, then No, calling Dispose() won’t do anything important. SemaphoreSlim will create a ManualResetEvent on demand if you access the AvailableWaitHandle. This may be useful, for example if you need to wait on multiple … Read more

Avoid calling Invoke when the control is disposed

What you have here is a race condition. You’re better off just catching the ObjectDisposed exception and be done with it. In fact, I think in this case it is the only working solution. try { if (mImageListView.InvokeRequired) mImageListView.Invoke(new YourDelegate(thisMethod)); else mImageListView.RefreshInternal(); } catch (ObjectDisposedException ex) { // Do something clever }

using statement FileStream and / or StreamReader – Visual Studio 2012 Warnings

The following is how Microsoft recommends doing it. It is long and bulky, but safe: FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (TextReader tr= new StreamReader(fs)) { fs = null; // Code here } } finally { if (fs != null) fs.Dispose(); } This method will always ensure … Read more