Skip to main content
 首页 » 编程设计

c#之当 Application.Run() 发生异常时,您有什么选择

2024年02月13日21kenshinobiy

我正在使用一个名为 GreatMaps.NET 的很棒库在我的应用程序中做一些映射。我真的很喜欢这个库(并强烈推荐它),但我偶尔会从一个内部 GreatMap.NET 例程中得到一个未处理的 ArgumentOutOfRangeException,它会一直跳到我的 Program.Main()。由于我的应用程序没有机会捕获并处理此错误,我是否应该接受此错误的存在?或者,我有什么办法可以避免这个问题吗?

我包含了调用堆栈,以防它显示我可能遗漏的内容。

System.ArgumentOutOfRangeException was unhandled 
  Message=Index was out of range. Must be non-negative and less than the size of the collection. 
Parameter name: index 
  Source=mscorlib 
  ParamName=index 
  StackTrace: 
       at System.ThrowHelper.ThrowArgumentOutOfRangeException() 
       at System.Collections.Generic.List`1.get_Item(Int32 index) 
       at GMap.NET.WindowsForms.GMapOverlay.DrawRoutes(Graphics g) 
       at GMap.NET.WindowsForms.GMapOverlay.Render(Graphics g) 
       at GMap.NET.WindowsForms.GMapControl.OnPaintOverlays(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) 
       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 Spectrum.Foxhunt.Client.Program.Main(String[] args) in C:\Users\Michael\Documents\Visual Studio 2010\Projects\Spectrum\Spectrum.Foxhunt.Client\Program.cs:line 23 
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) 
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() 
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) 
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) 
       at System.Activator.CreateInstance(ActivationContext activationContext) 
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
       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:  

请您参考如下方法:

如果这确实是该库中的错误,一种解决方案是创建一个派生自 GMapControl 的类并使用该类而不是原始类。

在派生类中,您将覆盖 OnPaint 并捕获该异常:

public class GMapControlFixed : GMapControl 
{ 
    public override void OnPaint(PaintEventArgs e) 
    { 
        try 
        { 
            base.OnPaint(e); 
        } 
        catch(ArgumentOutOfRangeException) 
        { 
            // discard - it's a bug in the original control. 
        } 
    } 
}