ObjectDisposedException at form close - vb.net

I have an app in visual studio 2005 sp1, implemented in motorola mc2100 with windows ce 6.0, the application works fine until you close it then gives an error:
Error:
vb_scansample2.exe (app name)
ObjectDisposedException
at
system.windows.forms.control.invokehelper(delegate method, boolean fsynchronous, object[] rgobjargs)
at
system.windows.forms.control.invokehelper(delegate method, object[] args)
at
symbol.barcode.ActionsEventFiringThread()
any help?

The barcode actions object is raising an event. That event is trying to update something in the UI, but the UI is already torn down. Wrap the event handler logic in a try/catch and ignore ObjectDisposedExceptions, or better yet, unhook the event when you're disposing.

Related

Can't view any Designer with a user Control on It

I'm using visual studios 2017, vb.net, winforms.
On every form that contains a user control I am getting the display:
"To prevent possible data loss before loading the designer, the following errors must be resolved:".
with the error message:
Error HRESULT E_FAIL has been returned from a call to a COM component.
I have tried creating a new user control and adding it to a new form and I get the same result. The display appears after reopening a form that has a user control on it.
The program itself works fine it's only the designer that I can not see.
I would appreciate any advice why this might be occurring as it doesn't seem to be code related.
It might possibly be related to me using resharper to remove any unused reference or me running it on my laptop (Also vs2017).
Thanks In Advance
The Stack Trace is:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at Microsoft.VisualStudio.LanguageServices.Implementation.Utilities.Exceptions.ThrowEFail()
at Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.CodeTypeRef.LookupTypeSymbol()
at Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.CodeTypeRef.get_TypeKind()
at EnvDTE.CodeTypeRef.get_TypeKind()
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.GetUrtTypeFromVsType(CodeTypeRef vsType)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnTypePopulateMembers(Object sender, EventArgs e)
at System.CodeDom.CodeTypeDeclaration.get_Members()
at Microsoft.VisualStudio.Design.Serialization.CodeDom.MergedCodeDomParser.CodeTypeDeclarationPopulator.OnPopulateMembers(Object sender, EventArgs e)
at System.CodeDom.CodeTypeDeclaration.get_Members()
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
Figure out the problem. For some reason there was a reference that pointed towards the project itself. The designer returned when I removed the reference.

VB.NET Application.Exit not closing forms opened using ShowDialog

I have a .Net application with multiple forms invoked using form.ShowDialog(). When I do Application.Exit, the application is not closing all the forms.
When I try to do an explicit Form.Close I am getting an error from WinForms as below
at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar) at System.Windows.Forms.Form._CloseModal() at System.Windows.Forms.Form.Close() at
As #CharithJ states in this answer (paraphrased):
Cancel the background worker and send an argument to the BackgroundWorker RunWorkerCompleted event to identify that time has expired. From that event you can call Application.Exit() just fine.

DataGridView.CurrentCellDirtyStateChanged throws InvalidProgramException when calling a method

I have a DataGridView that is bound to a DataTable. I was using AutoGenerateColumns=true but I changed it to false so that I could manage the columns myself and then I started getting an InvalidProgramException in the event handler for DataGridView.CurrentCellDirtyStateChanged. When this event handler calls another method called CheckFilter() it throws the exception. It does not enter the method when I step through the code so the exception happens when the method is called. CheckFilter() is a private sub with no parameters. The exception message is "Common Language Runtime detected an invalid program". I am using vb.net with .net 2.0 and Visual Studio 2008 on Windows 7.
It turns out that the exception did not come from calling the method but from a for each loop inside the method. I did not specify the type of the loop variable. The for each loop loops through a collection of datarows so I made the loop variable a DataGridViewRow and then the exception went away. I think it must be a bug in the framework or the compiler.

Unhandled exception in an even handler code block

I am coding a DLL in C++/CLI that is called in a C# based GUI application. I throw a custom exception (derived from Exception class), which, if not swallowed in my DLL, is handled in the final catch block in the GUI code. It works well for all throws, save one:
Im my DLL I have implemented a handler that will listen to SerialPort::DataReceived events. If I get an incorrect packet, I raise a custom exception, which is rethrown in the outermosst try-catch block. The GUI is supposed to display this custom exception. But for some reason an exception raised in this block is not re-thrown. Instead I get an "Unhandled exception" message in Debug mode. When I run the exe directly by double clicking the exe (in release folder) it simply crashes. Apparently, exceptions raised in this event handler method are not handled by the GUI. Or maybe they are not passed to the GUI from my DLL. It doesnt create a problem for other parts of my dll. The only reason I can think for this different behaviour is that the event handler doesn't have a caller. Is my guess correct? or is there some other reason. Any clues on how I can handle this problem? I dont want my application to crash. I just want to raise an exception so that the message can be displayed to the user in the GUI and the application can stop communicating with the serial port.
You need to switch to the UI thread before throwing from that event handler. This is typically accomplished with System::Threading::SynchronizationContext, but if you're using WinForms you can alternatively use System::Windows::Forms::Control::BeginInvoke or System::Windows::Forms::Control::Invoke.

Get an error System.ArgumentNullException was unhandled, Value Cannot be null. Parameter name: activationContext

I'm getting the following exception when I try to run up my Winforms app and haven't been able to find a solution to it:
System.ArgumentNullException was unhandled
Message=Value cannot be null.
Parameter name: activationContext
Source=mscorlib
ParamName=activationContext
StackTrace:
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I've added a handler for the "unhandled error" event in the applicationevents class but it never reaches that stage and there is no source code running that I can break into and diagnose.
The application was fine until I added a user control (nothing fancy, just a standard windows user control) and then dragged it onto a form in the app (user control and form were in the same application). The application builds and compiles without any errors or warnings but when I attempt to run it up I get the above error straight away.
Does anyone have any idea what it could be or even how I can go about investigating it? I'm a bit stumped on this one as I'm not sure how to investigate it. I removed the user control from the form and then I removed the user control from the application entirely but the error hasn't gone away. In all honesty the addition of the user control might be a red herring but the problem manifested straight after adding it.
Hope someone can help.
try unchecking the Enable clickonce security settings checkbox in the security tab of the application properties(Projects Tab -> "Project Name" Properties-> Security Tab).
Try Build -> Clean Solution instead. I don't know why this isn't done automatically during a Build, but its no good complaining
I also had this problem in VS2010 VB.NET, Thanks Rene!
Unchecking project_properties.security.enable click once removed my problem
How my symptoms manifested:
I had a custom user control on a form, and was forced to remove any reference to it manually inside InitializeComponent() subroutine
reason:
my Winform had corrupted just after I modified the New() subroutine of my custom user control, where I changed the New() to add arg's and I didn't create a 'default' Sub New() with no arg's, as required for winform page rendering)
Just to give closure on this question the following resolved it:
Unchecked the "enable clickonce security settings" checkbox in the security tab of the application properties.
As suggested by Rene in the comments of my question.