idisposable

ClientBase doesn't implement IDisposable member

空扰寡人 提交于 2019-12-05 21:29:52
问题 How is it possible for the System.ServiceModel.ClientBase abstract class to implement IDisposable Interface if the Dispose() Method declaration is not visible/declared? If I try to do the same I get an error and can't compile abstract class ATeste : IDisposable { } 'ATeste' does not implement interface member 'System.IDisposable.Dispose()' I'm using VS 2010 and Framework 4.0. Check the ClientBase declaration: // Summary: // Provides the base implementation used to create Windows Communication

Cancelling tasks in the Dispose method

拈花ヽ惹草 提交于 2019-12-05 20:59:53
问题 I have a class which spawns various tasks which can run indefinitely. When this object is disposed, I want to stop those tasks from running. Is this the correct approach: public class MyClass : IDisposable { // Stuff public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { queueCancellationTokenSource.Cancel(); feedCancellationTokenSource.Cancel(); } } } 回答1: You're on the right track. However, I would suggest

Correct way to pass Entity objects between layers?

断了今生、忘了曾经 提交于 2019-12-05 20:56:23
I am just learning the Entity Framework and have made some good progress on incorporating it with my layered code structure. I have 2 visual layers, a business layer and a data access layer. My problem is in passing entity object between layers. This code sample does not work: // BLL public static void Test1() { List<User> users = (from u in GetActiveUsers() where u.ID == 1 select u).ToList<User>(); // Do something with users } // DAL public static IQueryable<User> GetActiveUsers() { using (var context = new CSEntities()) { return from u in context.Users where u.Employee.FirstName == "Tom"

return the variable used for using inside the using C#

孤者浪人 提交于 2019-12-05 16:57:21
问题 I am returning the variable I am creating in a using statement inside the using statement (sounds funny): public DataTable foo () { using (DataTable properties = new DataTable()) { // do something return properties; } } Will this Dispose the properties variable?? After doing this am still getting this Warning: Warning 34 CA2000 : Microsoft.Reliability : In method 'test.test', call System.IDisposable.Dispose on object 'properties' before all references to it are out of scope. Any Ideas? Thanks

Dependency injection and life time of IDisposable objects

百般思念 提交于 2019-12-05 16:16:18
I am trying to develop a library using dependency injection approach (with Ninject) and I am having some kind of confusion likely because of my incorrect design. In summary, my design approach is A parent object has a common object. A parent object uses some variable number of child objects. All child objects should use the very same common object instance with their parent object Here is a simple model of my problem domain. interface IParent : IDisposable { void Operation(); } interface ICommon : IDisposable { void DoCommonThing(); } interface IChild1 { void DoSomething(); } interface IChild2

Question on IDisposable

。_饼干妹妹 提交于 2019-12-05 16:02:14
My question is why do I need IDisposable? I have a class that consumes some resources that need to be freed. I have two options Without IDisposable class SomeUtilityClass { public Stop() { // free resources } } With IDisposable class SomeUtilityClass, IDisposable { public void Dispose() { // free resources } } So why do I need IDisposable here? It does not matther how to name the function. class Program { public Main(..) { SomeUtilityClass _class = new SomeUtilityClass(); // clean up when we've done _class.Stop(); // OR _class.Dispose(); } } Because IDisposable is supported by C# and you can

Using using to dispose of nested objects

风流意气都作罢 提交于 2019-12-05 12:16:34
If I have code with nested objects like this do I need to use the nested using statements to make sure that both the SQLCommand and the SQLConnection objects are disposed of properly like shown below or am I ok if the code that instantiates the SQLCommand is within the outer using statement. using (var conn = new SqlConnection(sqlConnString)) { using (var cmd = new SqlCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = cmdTextHere; conn.Open(); cmd.Connection = conn; rowsAffected = cmd.ExecuteNonQuery(); } } Yes. You could clean it up a little like this using (SqlConnection conn

IDisposable implementation - What should go in 'if (disposing)'

你离开我真会死。 提交于 2019-12-05 11:39:18
问题 I have been fixing some memory leak issues in a winforms application and noticed some disposable objects that are not Disposed explicitly (developer hasn't called Dispose method). Implementation of Finalize method also doesn't help because it doesn't go in if (disposing) clause. All the static event unregistering and collection clearing have been put in if (disposing) clause. The best practice is calling the Dispose if the object is disposable, but unfortunately this happens sometimes If

Where to dispose resources in a System.Windows.Form-derived class?

别说谁变了你拦得住时间么 提交于 2019-12-05 08:29:18
I've got a form that creates a couple of disposable resources in its constructor that I need to dispose of. However, the C# form designer already produces a Dispose() method in the Designer.cs file that doesn't appear to have any type of user hook in it. So I'm at a loss as to how I'm supposed to implement the typical IDisposable pattern. This form is occasionally created but never shown, so using the Close event won't help. The objects in question are not IComponents , so I can't just add them to this.components . Where can I put this clean-up code and be sure it will run when the form is

Dispose StreamResourceInfo.Stream

笑着哭i 提交于 2019-12-05 07:29:27
I use StreamResourceInfo.Stream to get BitmapImage s from resources. Is it correct to Close and Dispose the stream after using it? I ask because in memory profiler, I get an error if I do so. Memory profiler says that a disposed instance has not been GCed. If I look on the web, I only can find this post to this topic. In this post, the responding person says, that it is meaninfull to dispose. However if I look at the circumstances and on the effect, I don't think that this is right. Does someone know what is the right action? Additional information: In the msdn examples I have seen, they don't