When to dispose textbox or similar controls in C# winforms [on hold] - c#

Whenever I refresh or save a winform with hundred textboxes 5-8 times its throwing error "A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll"
I tried to use dispose method but don't know where to use it


Exception thrown: 'System.OutOfMemoryException' in System.Drawing.dll

In very random cases when for example when I switch windows over my applications or even moving the mouse over some winform apear this redcross error on random places
Exception thrown: 'System.OutOfMemoryException' in System.Drawing.dll
Exception thrown: 'System.ArgumentException' in System.Drawing.dll
GetObjectType on this dc returned an invalid value.
How this can be pointed to. The code is very large. I am watching this bug rarely from couple of years now. I have to restart the app to make disappear this red cross. The form still function after error thrown.
GetObjectType on this dc returned an invalid value.
Exception thrown: 'System.ArgumentException' in System.Drawing.dll
GetObjectType on this dc returned an invalid value.
So I created an window for my presentations that follows the mouse and flickers. With this window over main application I can (accelerate) the call / trigger "out of memory" anywhere on my app. bump ...
Try checking the checkbox next to "except when thrown from System.Drawing.Dll and try and run it again.
What it looks like is that there are too many things being drawn on screen at the same time. Your computer runs out of memory and cannot handle it so draws a cross.
You'll need to try and optimize some of the drawing code somehow to fix this issue permanently.

Long running or debugging time in c#

My program contains thousands of lines.while debugging the program it will spend more time at last it show an exception 'System.ArgumentException' occurred in System.Drawing.dll "Parameter is not valid." -Picture Box control.some time it show the exception "memory overload".
I am not understanding why it is long debugging time.
Can any one help me?
The problem occurred only when including this statement (when creating the Form's object in custom user control):
Form1 mainfrm = new Form1();
Excessive delays during debugging followed by an out-of-memory exception often indicate that there is infinite recursion. Of could you would have to share the code to be sure, but that's where I'd check first.

Unhandled exception of type

After my game has been running for a certain amount of time, the same Warning shows up and my game breaks. I am not sure why this is and I cannot find a solution for it.
The message that keeps popping up is as follows:
Microsoft Visual Studio Express 2013 for Windows Desktop
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Drawing.dll
Additional information: The operation completed successfully
I have an Object class that deals with all of the Objects displayed on screen (position, collisions etc) and the warning seems to be coming from this class. When the warning is displayed, one line in-particular is highlighted every time, that is:
public Obj(Vector2 pos)
position = pos;
Any help resolving my problem will be appreciated
You have an exception in an unmanaged component of your game. Go to Debug -> Exceptions and tick the "user-unhandled" box in the Win32 Exceptions row.
This way, your debugger will break whenever a Win32 exception is thrown, and if you have the required symbols loaded, you'll be able to debug the code as you normally would.
Furthermore, you can obtain the last Win32 error code using the Marshal.GetLastWin32Error method. Armed with that information, you should be able to figure out what your code is throwing on precisely, and how to resolve it.

How do I know what in my code is triggering this 'System.ObjectDisposedException ?

I'm debugging and enhancing a C#/XAML program that repeatedly generates an exception while running which I can see looking at the Output window.
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
(for some reason they seem to happen in pairs) The program itself keeps running after the exceptions occur.
The program consists of some XAML/C# code-behind which forms a front-end and test harness for a library of calls in a .Net DLL, which is also part of our C# code.
The total code-base (front end and DLL) is about 100K lines of C# source code code spread among about 25 files. It mostly runs in one thread except for code which runs in socket-data handlers which get called by the system when data arrives from outside hardware devices (this program runs a factory manufacturing process).
How do I narrow-down/track-down where or what in my code is triggering these exceptions?
In Visual Studio you can break execution when an exception is thrown. Go to debug menu -> exceptions -> Common Language Runtime Exceptions -> System -> System.ObjectDisposedException and check the checkbox for "Thrown".
This exception indicates an attempt to access an object after it has been .Dispose()-ed. The reason the program continues to run is that the exception is handled. a First-Chance exception is always generated when an exception is thrown, even if it is handled with a catch statement.
In general, many first chance exceptions like this are not something to worry about. If you want to track them down, you will need to attach a first chance exception handler, and then use the StackTrace Property to find them. Assuming your Main function is in Program, the following code should work
static Program()
AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
{ // Breakpoint here
This will give you access to the exception via the e.Exception

NotSupportedException for form controls after receiving data with serial port

I am developing a program for Windows CE with visual studio 2008 . I am using Serialport data_received event to get data but after receiving data(string) when I want to set it to Textbox or Label 's Text property a NotSupportedException from TextBox.Text is throwing.
Instead, if I set a local variable with received data ,It works without any problem.
Exception :
An unhandled exception of type 'System.NotSupportedException' occurred in System.Drawing.dll
I hardly remember that years ago when I worked with sockets I had such problem which was related to threads !
can any one Help me with this?
The problem you're having is most likely that you cannot access user interface elements from any thread other than the main thread.
what you need to do is invoke code in the main thread to actually do the work of setting the properties you need.
this is done by declaring a delegate sub and passing it into control.invoke
see the following: