There is some way to catch eexceptions from external libraries?
I have an application in VB.NET, and all the code is controlled by Try..Catch blocks.
Also I am using some external libraries, some of them, GMap.net libraries.
The problem that I have with the libraries of GMap is that one of its libraries is throwing an exception which I am not able to control, because I have not access to the code of that librarie.
It is a 'Collection Modified..' exception, and maybe the developers of this librarie, in some place of the code, they forgot to control it.
now, what happens is that some times, my application throws an uncontrolled exception, which becames from that librarie, and I don't know how to catch it.
For sure there is some way to catch exceptions from external libraries, but I am not able to find the answer, so any help will be very grateful.
Here is the uncontrolled exception thrown by the application:
No se controló System.InvalidOperationException
HResult=-2146233079
Message=Collection was modified; enumeration operation may not execute.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List1.Enumerator.MoveNextRare()
at System.Collections.Generic.List1.Enumerator.MoveNext()
at GMap.NET.WindowsForms.GMapOverlay.OnRender(Graphics g)
at GMap.NET.WindowsForms.GMapControl.OnPaintOverlays(Graphics g)
at GMap.NET.WindowsForms.GMapControl.DrawGraphics(Graphics g)
at GMap.NET.WindowsForms.GMapControl.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, -Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.UserControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
Thank you very much.
Related
I have this code in my application:
My.Computer.FileSystem.CopyFile("D:\Temp\Origin.log", "D:\Temp\Destination.log", True)
This code used to work fine until 1 or 2 months ago.
The last couple of months it fails to execute and it throws a 'System.IO.IOException' in mscorlib.dll "Unsupported" when the destination files exists.
My target framework is 4.7.2
I tried to change target framework to 4.6.2 in case an update of NET framework broke it, but still I have the same issue.
Right now I had to temporarily bypass the problem like this:
If My.Computer.FileSystem.FileExists("D:\Temp\Destination.log") Then
My.Computer.FileSystem.DeleteFile("D:\Temp\Destination.log", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
End If
My.Computer.FileSystem.CopyFile("D:\Temp\Origin.log", "D:\Temp\Destination.log", True)
While the above code works, it defeats the purpose of having an overwrite overload (which is still supported in 4.7.2 AFAIK)
Also it means I have to change it in far too many places inside my application code.
Does anyone knows why my original code stopped working, or better yet, how to fix it ?
Edit:
Exception Stack:
System.IO.IOException
HResult=0x80070032
Message=The request is not supported.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
at Microsoft.VisualBasic.FileIO.FileSystem.CopyOrMoveFile(CopyOrMove operation, String sourceFileName, String destinationFileName, Boolean overwrite, UIOptionInternal showUI, UICancelOption onUserCancel)
at Microsoft.VisualBasic.MyServices.FileSystemProxy.CopyFile(String sourceFileName, String destinationFileName, Boolean overwrite)
at KT_Scan_Docs_Manager.Frm_Main.CreateFilelist(Int32 Meleti) in C:\Users\GTsag\Dropbox\Visual Studio\KT2-14 Scan Docs Manager\KT2-14 Scan Docs Manager\frm_Main.vb:line 2880
at KT_Scan_Docs_Manager.Frm_Main.Bt_KT214CreateFileList_Click(Object sender, EventArgs e) in C:\Users\GTsag\Dropbox\Visual Studio\KT2-14 Scan Docs Manager\KT2-14 Scan Docs Manager\frm_Main.vb:line 148
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at KT_Scan_Docs_Manager.My.MyApplication.Main(String[] Args) in :line 81
I trying to find out why a VB.NET application has stopped working and gives the following error when opening:
************** Exception Text ************** System.NullReferenceException: Object reference not set to an instance
of an object. at System.Object.GetType() at
OneFrm1.Form1.sub_something(Object array1, Object O1, Object O2) at
OneFrm1.Form1.add_square_op1_to_array(square_area_options& array1,
String v) at OneFrm1.Form1.sub_something(Object array1, Object O1,
Object O2) at OneFrm1.Form1.load_date_panel_format_array() at
OneFrm1.Form1.Form1_Load(Object sender, EventArgs e) at
System.EventHandler.Invoke(Object sender, EventArgs e) at
System.Windows.Forms.Form.OnLoad(EventArgs e) at
System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl() at
System.Windows.Forms.Control.WmShowWindow(Message& m) at
System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
The application works fine if I right click and run as 'another user' but gives the above error if running as the logged-in user. I have tried a new user profile and it still gives the same error. The same issue with security software removed.
The other user has the same rights.
I've just inherited a legacy VB.Net project which runs fine in VS Community 2017 on a machine on the client's network, but when I try and run the code locally (on VS2017 Pro) I get an exception.
The project has the following properties set:
Splash screen: SplashInvoice
Startup form: SetupWizard
The code in SetupWizard form's load event checks a few thing and then closes itself if they are OK and opens another form:
Private Sub SetupWizard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If [some conditions] Then
frmMain.Show()
Me.Close()
Else
I'm getting this error when execution exits the SetupWizard_Load if the conditions are met and me.close is called:
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Windows.Forms.dll Cross-thread operation not valid:
Control 'SplashInvoice' accessed from a thread other than the thread it
was created on.
If I comment out the me.close bit it all works fine.
So, it seems like closing the project's startup form while the project's splash screen is still showing is the issue, which leads to a couple of questions:
Why does that cause an exception - isn't this all running on the main
thread of execution?
Why is this happening on my machine, but not on
the dev machine?
This is the full stack trace:
System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled
exceptionInvoice
Generator.exeSystem.InvalidOperationException,
mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089Cross-thread
operation not valid: Control 'SplashInvoice' accessed from a thread other
than the thread it was created on. at
System.Windows.Forms.Control.get_Handle() at
System.Windows.Forms.Form.Activate() at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.HideSplashScreen()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.MainFormLoadingDone(Object
sender, EventArgs e) at System.EventHandler.Invoke(Object sender,
EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) at
System.Windows.Forms.Form.OnCreateControl() at
System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl() at
System.Windows.Forms.Control.WmShowWindow(Message& m) at
System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m) at
System.Windows.Forms.Form.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&
m) at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr
lparam)System.InvalidOperationException:
Cross-thread operation not valid: Control 'SplashInvoice' accessed from a
thread other than the thread it was created on. at
System.Windows.Forms.Control.get_Handle() at
System.Windows.Forms.Form.Activate() at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.HideSplashScreen()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.MainFormLoadingDone(Object
sender, EventArgs e) at System.EventHandler.Invoke(Object sender,
EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) at
System.Windows.Forms.Form.OnCreateControl() at
System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl() at
System.Windows.Forms.Control.WmShowWindow(Message& m) at
System.Windows.Forms.Control.WndProc(Message& m) at
System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m) at
System.Windows.Forms.Form.WndProc(Message& m) at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&
m) at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr
lparam) An unhandled
exception of type 'System.InvalidOperationException' occurred in
System.Windows.Forms.dll Cross-thread operation not valid: Control
'SplashTest' accessed from a thread other than the thread it was
created on.
The program '[50096] Invoice Generator.exe' has exited with code -1
(0xffffffff).
Before really understanding what was going on here I did manage to stop the exception by manually closing the splash screen in the start up form's Load event. I added a call to the following method into that event:
private sub CloseSplash()
Dim mySplash = My.Application.OpenForms.Item("SplashInvoice")
mySplash.Invoke(New MethodInvoker(Sub()
mySplash.Close()
mySplash.Dispose()
End Sub))
End sub
That code is based on this answer.
#TnTinMn then provided some really useful information on all of this the comments and led to me to the Microsoft documentation on Extending the Visual Basic Application Model
That has this excellent diagram to explain how it all wires together:
That article also states
ShowSplashScreen. Determines if the application has a splash screen
defined and if it does, displays the splash screen on a separate
thread.
Which appears to the be root of the issue I'm seeing.
#TnMinMn also made some really useful suggestions on how to fix this issue:
Personally, I would disable the Application Framework and use a Sub
Main to launch a customized version of
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.
That way you could set the MainForm based on your some conditions,
while still utilizing the base splash screen support, instead of
creating a form that never shows when some conditions is true
IT;S VB.NET not C# and i don't know how to exchange source from C# to VB.NET, THANKS
I want to get all paths from all running processes.
This is my source so far:
For Each p As Process In Process.GetProcesses
Try
ListBox1.Items.Add(p.MainModule.FileName.ToString)
Catch ex As Exception
RichTextBox1.Text = ex.Message
End Try
Next
But I can't get all path folders of the running processes.
If I examine the ex.Message, the response is like this
Unable to enumerate the process modules.
But if I not using ex.Message, the response is like this :
System.ComponentModel.Win32Exception was unhandled
ErrorCode=-2147467259
HResult=-2147467259
Message=A 32 bit processes cannot access modules of a 64 bit process.
NativeErrorCode=299
Source=System
StackTrace:
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at Anti_Cheat.Form1.Button6_Click(Object sender, EventArgs e) in c:\users\adiyatma\documents\visual studio 2012\Projects\Anti Cheat\Anti Cheat\Form1.vb:line 40
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Anti_Cheat.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Can anyone help me?
Take a look at the so-question How to get the full path of running process?
Instead of
ListBox1.Items.Add(p.MainModule.FileName.ToString)
try
ListBox1.Items.Add(p.Modules(0).FileName.ToString)
Edit:
Have you tried to evaluate the property of different processes directly? Maybe there is a certain process you cannot access, resulting in the error described.
You can try to iterate trough the processes one by one by creating the following loop:
For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
Try
Console.WriteLine(p.Modules(0).FileName)
Catch ex As Exception
Console.WriteLine(String.Format("{0}: Error - {1}", p.ProcessName, ex.Message))
End Try
Next
By doing so, you should be able to determine the processes you're not allowed to access and get a couple of processes you should be able to experiment with.
Mate the problem is obvious, you are targeting 32-bit but are testing your app on a computer that has 64-bit installed system, that's why you get the error.
Message=A 32 bit processes cannot access modules of a 64 bit process.
to fix this issue you should make the target 64-bit, there IS NO SOLUTION for this...
if you know the famous procexp (sysinternals) it has two separate application, when the system is 32-bit it lauches an instance of 32-bit, but when it is 64-bit it lauches another separate process for the system...
so if you want to deal with this issue, you have to make two instances for system compability,
Hope this is helpful
I've seen many topics about this problem, but none of them refers to my particular case.
The problem is that (not when debugging) but after application installation in a test environment i receive the following error. Since it does not occur while debugging I have no idea where is the problem.
The most funny thing is that I removed everything from the splash form as far as code. There is nothing there.
JIT debugger stuff:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.HideSplashScreen()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.MainFormLoadingDone(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Can I somehow block all errors coming from the splash screen since nothing is there?
Pete