dispose

Application.Run throws ArgumentException was unhandled

倾然丶 夕夏残阳落幕 提交于 2019-12-08 04:16:05
问题 I have a condition in which I need to close the application and so I call this.Dispose () when I set a certian flag. At first I thought it was a problem of calling functions after I call this.Dispose () and so I moved the code to be the last thing called, but I still get an "ArgumentException was unhandled" "Parameter is not valid." On the Application.Run (new myApp (); line. What am I doing wrong? Did I miss something along the way? Or maybe there is a better way to close the application?

ObjectDisposedException when outputting to console

依然范特西╮ 提交于 2019-12-08 03:40:15
问题 If I have the following code, I have no runtime or compilation problems: if (ConsoleAppBase.NORMAL_EXIT_CODE == code) { StdOut.WriteLine(msg); } else { StdErr.WriteLine(msg); } However, in trying to make this more concise, I switched to the following code: (ConsoleAppBase.NORMAL_EXIT_CODE == code ? StdOut : StdErr ).WriteLine(msg); When I have this code, I get the following exception at runtime: System.ObjectDisposedException: Cannot write to a closed TextWriter Can you explain why this

Disposing JFrame from another class

ⅰ亾dé卋堺 提交于 2019-12-08 03:09:53
问题 How can I dispose JFrame from another class? My code is listed below. public class MainWindow { JFrame main_f = new JFrame("xx"); main_f.getContentPane().setLayout(new BorderLayout()); main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); . . . } Disposing class: public static void DisposingJFrame (){ . . . MainWindow.main_f.dispose(); } MainWindow.main_f.dispose() won't work because main_f isn't a variable. Can you help me? 回答1: Suggestion : Make the JFrame instance a field of the

Is disposing a panel equivalent to disposing its children plus itself?

时间秒杀一切 提交于 2019-12-08 01:28:19
问题 Within the following class: class MyPanel : Panel { ... protected override void Dispose(bool disposing) { // My code here } } are the following two code samples equivalent? base.Dispose(disposing); vs if (disposing) { List<Control> ctrls = new List<Control>(this.Controls); this.Controls.Clear(); foreach(Control c in ctrls) { c.Dispose(); } } base.Dispose(disposing); If they have a different effect, what would it be? Edit: I ask this because, for whatever reason, doing it the first way freezes

C# Dispose : when dispose and who dispose it

南楼画角 提交于 2019-12-08 00:51:28
问题 I'm getting in nerve between dispose and finalize. Here is my example code: public class Car:IDisposable { public string name; public Car() { name = "My Car"; } public void Dispose() { Console.WriteLine("This object has been disposed"); } } public static void Main() { Car anotherCar; using (var car = new Car()) { anotherCar = car; Console.WriteLine("Before dispose. Name is: "+anotherCar.name); } Console.WriteLine("After dispose. Name is: "+anotherCar.name); } The result is: Before dispose.

Understanding disposable objects

筅森魡賤 提交于 2019-12-07 17:26:51
问题 I've looked in SO about a question like this one, and even that I've found quite a few, any of those threw any light into this matter for me. Let's assume I have this code: public class SuperObject : IDisposable { public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { } } Do I need the protected virtual void Dispose(bool) on SuperObject ? Since there is really nothing to dispose there. public interface ICustom : IDisposable { }

Disposing dynamically created controls

北战南征 提交于 2019-12-07 14:28:13
问题 I have a WinForms TabControl that I am dynamically adding TabPages to at runtime. Each TabPage contains a WebBrowser control. I also have the ability to remove the TabPages at runtime. Should I bother Dispose()ing the TabPage and/or WebBrowser controls? It seems to me I should at least Dispose() the WebBrowser control since it is a bit of a resource hog. 回答1: You should Dispose() your tab page when you remove it. This will automatically dispose all of the child controls. For details, see the

Dispose of object more than one time error. CA2202. Is there a better way?

拟墨画扇 提交于 2019-12-07 10:59:03
问题 How can I ensure the following code is disposing of all objects in a better fashion? Currently, Code Analysis is telling me Error 45 CA2202 : Microsoft.Usage : Object 'ns' can be disposed more than once in method 'CPCommunicator.GetResults(string)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.: Lines: 64, 65 NetworkStream ns = null; StreamWriter requestStream = null; TextReader responseStream = null; var results = new

How do I know if UdpClient has been closed/disposed?

﹥>﹥吖頭↗ 提交于 2019-12-07 10:29:30
问题 I am receiving data from UdpClient via the usual async callback: private void OnUdpData(IAsyncResult result) { byte[] data = _udpReceive.EndReceive(result, ref _receiveEndPoint); //Snip doing stuff with data _udpReceive.BeginReceive(OnUdpData, null); } When I Close() the UdpClient in the main thread, the callback fires as I would expect, but at this point _udpReceive is already disposed and I get an ObjectDisposedException when I try and call EndReceive() . I was expecting to just get an

Tricky IDisposable Issue

瘦欲@ 提交于 2019-12-07 08:23:45
问题 I'm trying to abstract/encapsulate the following code so all client calls don't need to repeat this code. For example, this is a call, from a view model (MVVM) to a WCF service: using (var channelFactory = new WcfChannelFactory<IPrestoService>(new NetTcpBinding())) { var endpointAddress = ConfigurationManager.AppSettings["prestoServiceAddress"]; IPrestoService prestoService = channelFactory.CreateChannel(new EndpointAddress(endpointAddress)); this.Applications = new ObservableCollection